﻿#include "pch.h"

CResultCaculator::CResultCaculator()
{
    memset(this, 0, sizeof(CResultCaculator));
}

void CResultCaculator::printSelf(char *captionUpper, int32_t deep)
{
    char caption[1024];
    sprintf(caption, "%s:%s", captionUpper, "CResultCaculator");
    print4x(this, sizeof(CResultCaculator), caption);
    if (this->head)
    {
        sprintf(caption, "%s:%s", captionUpper, "CResultCaculator.head");
        print4x(this->head, sizeof(TDLLResultListItem), caption);
    }
    if (this->pTDLLItem)
    {
        sprintf(caption, "%s:%s", captionUpper, "CResultCaculator.pTDLLItem");
        print4x(this->pTDLLItem, sizeof(TDLLResultListItem), caption);
    }
    if (--deep)
    {
        sprintf(caption, "%s:%s", captionUpper, "CResultCaculator");
        if (this->pIndexWrapper)
        {
            this->pIndexWrapper->printSelf(caption, deep);
        }
        if (this->pCTickmarkManager)
        {
            this->pCTickmarkManager->printSelf(caption, deep);
        }
        if (this->pBuffer10Wrapper)
        {
            this->pBuffer10Wrapper->printSelf(caption, deep);
        }
        if (this->pEventManager)
        {
            this->pEventManager->printSelf(caption, deep);
        }
        if (this->pResInfWrapper)
        {
            this->pResInfWrapper->printSelf(caption, deep);
        }
    }
    if (this->pAnalInfo)
    {
        sprintf(caption, "%s:%s", captionUpper, "CResultCaculator.pAnalInfo");
        print4x(this->pAnalInfo, sizeof(int32_t) + sizeof(CODE) + 0x400u, caption);
    }
    if (this->pRawBagBuffer)
    {
        sprintf(caption, "%s:%s", captionUpper, "CResultCaculator.pRawBagBuffer");
        print4x(this->pRawBagBuffer, 1024, caption);
    }
}

// sub_100036B8
CResultCaculator *CResultCaculator::init(CTickmarkManager *pTickmarkManager, CBuffer10Wrapper *pBuffer10Wrapper,
                                         CEventManager *pEventManager, CResInfWrapper *pResInf, AnalInfo *pAnalInfo,
                                         int32_t *pRawBag, AnalExecutor *pX)
{
    this->pCTickmarkManager = pTickmarkManager;
    this->pBuffer10Wrapper = pBuffer10Wrapper;
    this->pEventManager = pEventManager;
    this->pResInfWrapper = pResInf;
    this->pAnalInfo = pAnalInfo;
    this->pRawBagBuffer = pRawBag;
    this->pAnalExecutor = pX;
    this->pIndexWrapper = new CIndexWrapper();
    this->pIndexWrapper->init(128);
    this->xMinUnitK = 60.0;
    this->pResInfWrapper->rawbagFreq = this->pEventManager->rawbagFreq;
    return this;
}
CResultCaculator *CResultCaculator::init_()
{
    return this;
}

// sub_1001B220
void *CResultCaculator::close(char flag)
{
    this->close();
    if (flag & 1)
    {
        delete this;
        return NULL;
    }
    return this;
}

// sub_1001B250
void *CResultCaculator::close()
{
    void *result; // eax

    result = this;
    if (this->pIndexWrapper)
    {
        result = (void *)pIndexWrapper->close(1);
    }
    return result;
}

// sub_1001B2E0
CResultCaculator *CResultCaculator::clean()
{
    pIndexWrapper->initData();
    return this->initData();
}

// sub_10003796
CResultCaculator *CResultCaculator::initData()
{
    this->head = pResInfWrapper->getHead();
    this->pTDLLItem = (TDLLResultListItem *)this->head;
    *(pResInfWrapper->getNumPointer()) = 0;
    this->untreatedNum = this->pResInfWrapper->getLen();
    this->baseIdx = 0;
    this->tickmarkStatus = false;
    this->typeCodeStatus = false;
    this->finishFlag = false;
    this->storedTE = 0;
    this->topBottomHS = -1;
    this->smoothAreaShift = 0;
    return this->clean_();
}

// sub_1001A170
CResultCaculator *CResultCaculator::clean_()
{
    this->tempArea = 0;
    this->tempSlope = 0;
    this->tempTime = 0;
    this->tempHeight = 0;
    this->pCalcItem = NULL;
    return this;
}

// 10003B09
int32_t CResultCaculator::checkResInfoNum(int32_t num)
{
    int32_t sum;
    int32_t result;

    if (num < 0)
    {
        num = this->baseIdx;
    }
    result = true;
    if (num < this->untreatedNum)
    {
        sum = num + this->pResInfWrapper->getNum();
        if (sum < this->pResInfWrapper->getLen())
        {
            result = 0;
        }
    }
    return result;
}

// sub_1001B9F0
int32_t CResultCaculator::isLocalDiffHExtruded1D()
{
    float normSlope;
    float normDiffH05;

    normSlope = this->pEventManager->getNormalizeSlope();
    normDiffH05 = (float)this->pBuffer10Wrapper->getNormalDiffH05();
    if (this->pEventManager->topOrBottom)
    {
        normDiffH05 = (float)(0.0 - normDiffH05);
    }
    return normDiffH05 >= (double)normSlope;
}

// sub_100083BB
void CResultCaculator::setPeakStart(int32_t time, int32_t height, double area)
{
    this->pTDLLItem->ts = time;
    this->pTDLLItem->hs = (float)height;
    this->pTDLLItem->Areas = area;
}

// sub_10008374
void CResultCaculator::setPeakEnd(int32_t time, int32_t height, double area)
{
    this->pTDLLItem->te = time;
    this->pTDLLItem->he = (float)height;
    this->pTDLLItem->Areas = area;
    this->pTDLLItem->TypeCode[0] = C78;
}

// sub_1001A450
int32_t CResultCaculator::processIndexAndTickmark(int32_t offset)
{
    if (this->pEventManager->evCount[BASE_TOGETHER])
    {
        return false;
    }
    this->setIndex1AndTickmark(offset);
    return this->finishBatchHandle(offset);
}

// unknown_libname_4
int32_t CResultCaculator::getTickmarkStatus()
{
    return this->tickmarkStatus;
}

// sub_100052C0
int32_t CResultCaculator::setIndex1AndTickmark(int32_t offset)
{
    int32_t result;
    int32_t tail;
    int32_t head;
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    result = this->isOffsetOutBound(idx);
    if (!result)
    {
        if (this->pIndexWrapper->isIndex1Valid())
        {
            head = this->pIndexWrapper->indexBuf->head;
            pHead[head].TypeCode[2] = C66;
            this->setIndex1EndResult(offset);
            if (this->pIndexWrapper->getNum() > 0)
            {
                IndexBuffer1 *pIndex = (IndexBuffer1 *)this->pIndexWrapper->head;
                tail = pIndex[this->pIndexWrapper->getNum() - 1].tail;
                if (tail < idx)
                {
                    pHead[head].TypeCode[2] = C86;
                    pHead[tail + 1].TypeCode[1] = C86;
                }
            }
        }
        this->tickmarkStatus = false;
        this->typeCodeStatus = false;
        this->pIndexWrapper->baseTail = false;
        pHead[idx].TypeCode[2] = C66;
        this->pCTickmarkManager->setFirstTickmark(pHead[idx].te, STOP_TICK);
        result = this->isAreaShiftPositive(idx, true);
    }
    return result;
}

// sub_10005D8D
int32_t CResultCaculator::finishBatchHandle(int32_t offset)
{
    int32_t len;
    char *errMsg;
    char *Source;
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    this->typeCodeStatus = false;
    this->finishFlag = true;
    this->pAnalInfo->status = 0;
    this->baseIdx = idx + 1;
    this->pTDLLItem = &pHead[this->baseIdx];
    if (!this->isOffsetOutBound(idx))
    {
        return true;
    }
    if (gLanguage == LANG_EN)
    {
        Source = aNumberOfPeaksO0_EN;
    }
    else
        Source = (char *)&aNumberOfPeaksO0_CN;
    len = this->pResInfWrapper->getLen();
    errMsg = formatExceedError(Source, len);
    strncpy(this->pAnalInfo->errMsg, errMsg, len + 1);
    this->pAnalInfo->code = ANAL_ERROR;
    return false;
}

// sub_10003B09
int32_t CResultCaculator::isOffsetOutBound(int32_t offset)
{
    int32_t idx;
    int32_t result;

    if (offset < 0)
    {
        offset = this->baseIdx;
    }
    result = true;
    if (offset < this->untreatedNum)
    {
        idx = offset + this->pResInfWrapper->getNum();
        if (idx < this->pResInfWrapper->getLen())
        {
            result = false;
        }
    }
    return result;
}

// sub_10003AB5
int32_t CResultCaculator::isOffsetOutBoundV2(int32_t offset)
{
    int32_t idx;
    int32_t result;

    if (offset < 0)
    {
        offset = this->baseIdx;
    }
    result = true;
    if (offset < this->untreatedNum - 1)
    {
        idx = offset + this->pResInfWrapper->getNum();
        if (idx < this->pResInfWrapper->getLen() - 1)
        {
            result = false;
        }
    }
    return result;
}

// sub_10005602
int32_t CResultCaculator::setIndex1EndResult(int32_t offset)
{
    int32_t num;
    int32_t head;
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    idx = offset + this->baseIdx;
    if (this->isOffsetOutBound(idx))
    {
        return false;
    }
    if (this->pIndexWrapper->isIndex2Valid())
    {
        if (idx >= this->pIndexWrapper->indexBuf2->head2 && pHead[idx].he <= pHead[this->pIndexWrapper->indexBuf2->head2].hs)
        {
            this->setTanStopAndIncIndexBuf2(offset);
        }
        else
        {
            this->setSOL_TICK();
        }
    }
    if (!this->pIndexWrapper->isIndex1Valid())
    {
        return false;
    }
    head = this->pIndexWrapper->indexBuf->head;
    pHead[head].te = pHead[idx].te;
    pHead[head].he = pHead[idx].he;
    pHead[head].Areae = pHead[idx].Areae;
    if (pHead[head].TypeCode[2] == C32 || !pHead[head].TypeCode[2])
    {
        pHead[head].TypeCode[2] = C86;
    }
    this->pIndexWrapper->indexBuf->tail = idx;
    this->pIndexWrapper->indexBuf->itemCount = this->pIndexWrapper->currentTail2;
    this->pIndexWrapper->index1Flag = 0;
    num = this->pIndexWrapper->getNum();
    if (num < this->pIndexWrapper->getLen())
    {
        this->pIndexWrapper->indexBuf++;
        this->pIndexWrapper->indexBuf->tail = -1;
        this->pIndexWrapper->indexBuf->head = -1;
        this->pIndexWrapper->indexBuf->itemCount = 0;
        this->pIndexWrapper->indexBuf->pIndex2 = NULL;
    }
    return true;
}

// sub_1000676F
void CResultCaculator::setSOL_TICK()
{
    int32_t i;
    int32_t idxHead;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    IndexBuffer1 *pIndex = this->pIndexWrapper->indexBuf;
    IndexBuffer2 *pIndex2 = this->pIndexWrapper->indexBuf2;

    if (this->pIndexWrapper->indexBuf2)
    {
        idxHead = this->pIndexWrapper->indexBuf2->head2;
        if (idxHead >= 0)
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[idxHead].ts, TAN_START_TICK);
            this->pIndexWrapper->index1Flag = 0;
            pHead[idxHead].TypeCode[1] = C86;
            for (i = idxHead; i <= this->baseIdx; ++i)
            {
                pHead[i].TypeCode[0] = C32;
            }
        }
        pIndex2->tail2 = -1;
        pIndex2->head2 = -1;
        --this->pIndexWrapper->currentTail2;
        if (this->pIndexWrapper->cleanIndexBuf2())
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[pIndex->head].ts, SOL_TICK);
            pHead[pIndex->head].TypeCode[0] = C32;
            pHead[pIndex->head].TypeCode[2] = C86;
            pIndex->tail = -1;
            pIndex->head = -1;
            pIndex->itemCount = 0;
            pIndex->pIndex2 = NULL;
            if (this->pIndexWrapper->numValue > 0)
            {
                --this->pIndexWrapper->numValue;
            }
            this->pIndexWrapper->index1Flag = 0;
        }
        else
        {
            this->setIndex1EndResult(idxHead - this->baseIdx - 1);
        }
    }
    else
    {
        if (pIndex)
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[pIndex->head].ts, SOL_TICK);
            pHead[pIndex->head].TypeCode[0] = C32;
            pHead[pIndex->head].TypeCode[2] = C86;
            pIndex->tail = -1;
            pIndex->head = -1;
            pIndex->itemCount = 0;
            pIndex->pIndex2 = NULL;
            if (this->pIndexWrapper->numValue > 0)
            {
                --this->pIndexWrapper->numValue;
            }
            this->pIndexWrapper->index1Flag = 0;
        }
    }
}

// sub_1000595D
int32_t CResultCaculator::setTanStopAndIncIndexBuf2(int32_t offset)
{
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    if (!this->isOffsetOutBound(idx))
    {
        if (pHead[idx].TypeCode[2] == C86)
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[idx].te, VALLEY_TICK);
        }
        this->pCTickmarkManager->setFirstTickmark(pHead[idx].te, TAN_STOP_TICK);
        this->pIndexWrapper->indexBuf2->tail2 = idx;
        pHead[idx].TypeCode[2] = C66;
        this->isAreaShiftPositive(idx, true);
    }
    this->tickmarkStatus = true;
    if (this->pIndexWrapper->currentTail2 < this->pIndexWrapper->indexCount2)
    {
        this->pIndexWrapper->indexBuf2++;
        this->pIndexWrapper->indexBuf2->tail2 = -1;
        this->pIndexWrapper->indexBuf2->head2 = -1;
    }
    return true;
}

// sub_10003DCA
int32_t CResultCaculator::isAreaShiftPositive(int32_t offset, int32_t flag)
{
    int32_t areaDiffShift;
    int32_t areaDiffShift0;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (this->isOffsetOutBound(offset))
    {
        return false;
    }
    if (this->getBaseTailTangant())
    {
        return false;
    }
    if (!(int32_t)pHead[offset].tr && !(int32_t)pHead[offset].hr && !(int32_t)pHead[offset].Arear)
    {
        return false;
    }
    areaDiffShift = (int32_t)this->calcAreaDiffShift(offset, flag);
    if (areaDiffShift <= 0)
    {
        return false;
    }
    if (flag)
    {
        areaDiffShift0 = (int32_t)this->calcAreaDiffShift(offset, 0);
        if (areaDiffShift0 > 0 && areaDiffShift > 3 * areaDiffShift0)
        {
            areaDiffShift = 3 * areaDiffShift0;
        }
    }
    else if ((int32_t)this->smoothAreaShift > 0 && areaDiffShift > 3 * this->smoothAreaShift)
    {
        areaDiffShift = 3 * this->smoothAreaShift;
    }
    this->smoothAreaShift = (3 * areaDiffShift + this->smoothAreaShift) / 4;
    this->pEventManager->calcWidth0(this->smoothAreaShift);
    return true;
}

// sub_1001A1E0
int32_t CResultCaculator::getBaseTailTangant()
{
    return this->pIndexWrapper->baseTail;
}

// sub_10003CA9
float CResultCaculator::calcAreaDiffShift(int32_t offset, int32_t flag)
{
    TDLLResultListItem *pRes, *fp;
    int32_t ret;
    double areaDiff;

    pRes = (TDLLResultListItem *)this->head;
    fp = &pRes[offset];

    if (flag || fp->hs + 1.0 >= fp->hr)
    {
        if (flag == 1 && fp->he + 1.0 < fp->hr)
        {
            areaDiff = fp->Areae - fp->Arear;
            ret = (int32_t)((areaDiff - (getFte(fp) - fp->tr) * fp->he) * 1.878 / (fp->hr - fp->he));
        }
        else
        {
            ret = 0;
        }
    }
    else
    {
        areaDiff = fp->Arear - fp->Areas;
        ret = (int32_t)((areaDiff - (fp->tr - getFts(fp)) * fp->hs) * 1.878 / (fp->hr - fp->hs));
    }
    return (float)ret;
}

// sub_100088EC
int32_t CResultCaculator::calcResultAndSetBaseIdx()
{
    int32_t result;
    int32_t finishedNum;
    TDLLResultListItem *resInfHead = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    int32_t len;
    float freq;
    int32_t finishedOffs;
    int32_t offs;

    if (!this->finishFlag || !this->baseIdx)
    {
        return true;
    }
    finishedOffs = this->baseIdx - 1;
    finishedNum = this->pResInfWrapper->getNum() + finishedOffs + 1;
    if (finishedNum <= this->pResInfWrapper->getLen())
    {
        this->calcAreaTimeHeight(NULL);
        this->setHeightValues(this->pRawBagBuffer);
        freq = (float)(this->pEventManager->getRawbagFreqValue());
        offs = this->calcResultValue(finishedOffs, freq);
        this->pResInfWrapper->increaseNum(offs + 1);
        resInfHead = (TDLLResultListItem *)this->pResInfWrapper->getHead();
        this->head = (LPVOID)&resInfHead[this->pResInfWrapper->getNum()];
        this->pTDLLItem = (TDLLResultListItem *)this->head;
        len = this->pResInfWrapper->getLen();
        this->untreatedNum = len - this->pResInfWrapper->getNum();
        if (this->untreatedNum > 0)
        {
            memset(this->head, 0, sizeof(TDLLResultListItem));
        }
        this->finishFlag = false;
        this->baseIdx = 0;
        result = true;
    }
    else
    {
        --this->baseIdx;
        result = false;
    }
    return result;
}

// sub_10008A2D
int32_t CResultCaculator::calcResultValue(int32_t offset, float freq)
{
    int32_t i;
    int32_t j;
    int32_t currVal;
    int32_t preVal;
    int32_t time;
    float time0;
    int32_t idxAll;
    int32_t idxFrom;
    float ftsAll;
    float ftsFrom;
    int32_t tail;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (offset < 0)
    {
        return offset;
    }
    if (this->pEventManager->evType[PARA_NOISE])
    {
        for (i = 0; i <= offset; ++i)
        {
            pHead[i].TypeCode[0] = C78;
        }
    }
    for (j = 0; j <= offset; ++j)
    {
        pHead[j].fts = 0;
        pHead[j].fte = 0;
    }
    if (this->pEventManager->evType[PARA_DRIFT] && pHead[0].TypeCode[0] == C78)
    {
        if (this->head <= this->pResInfWrapper->head || pHead[-1].TypeCode[0] == C78 || pHead[-1].fte <= 0.0)
        {
            if (*this->pEventManager->hsMin < pHead[0].hs)
            {
                time = pHead[0].ts;
                currVal = this->pRawBagBuffer[time];
                preVal = this->pRawBagBuffer[time - 1];
                if (currVal == preVal)
                {
                    time0 = 0.0;
                }
                else
                {
                    time0 = (float)((double)(currVal - *this->pEventManager->hsMin) / (double)(currVal - preVal));
                }
                pHead[0].fts = (float)((double)time - time0);
                pHead[0].Areas = pHead[0].Areas - *this->pEventManager->hsMin * time0;
                pHead[0].hs = (float)(*this->pEventManager->hsMin);
            }
        }
        else
        {
            pHead[0].fts = pHead[-1].fte;
            pHead[0].Areas = pHead[-1].Areae;
            pHead[0].hs = pHead[-1].he;
        }
        time = pHead[offset].te;
        currVal = this->pRawBagBuffer[time];
        preVal = this->pRawBagBuffer[time - 1];
        if (currVal == preVal)
        {
            time0 = 0.0;
        }
        else
        {
            time0 = (float)((double)(currVal - *this->pEventManager->hsMin) / (double)(currVal - preVal));
        }
        pHead[offset].fte = (float)((double)time - time0);
        pHead[offset].Areae = pHead[offset].Areae - *this->pEventManager->hsMin * time0;
        pHead[offset].he = (float)(*this->pEventManager->hsMin);
    }
    else
    {
        if (this->head > this->pResInfWrapper->head && pHead[-1].TypeCode[0] == C78 && pHead[-1].fte > 0.0)
        {
            pHead[0].fts = pHead[-1].fte;
            pHead[0].Areas = pHead[-1].Areae;
            pHead[0].hs = pHead[-1].he;
        }
        if (this->pEventManager->evType[PARA_DRIFT] && *this->pEventManager->hsMin > pHead[offset].he)
        {
            time = pHead[offset].te;
            currVal = this->pRawBagBuffer[time];
            preVal = this->pRawBagBuffer[time - 1];
            if (currVal == preVal)
            {
                time0 = 0.0;
            }
            else
            {
                time0 =
                    (float)((double)(currVal - *this->pEventManager->hsMin) / (double)(currVal - preVal));
            }
            pHead[offset].fte = (float)((double)time - time0);
            pHead[offset].Areae = pHead[offset].Areae - *this->pEventManager->hsMin * time0;
            pHead[offset].he = (float)(*this->pEventManager->hsMin);
        }
    }
    idxAll = searchSucceedAll((TDLLResultListItem *)this->head, offset);
    idxFrom = searchSucceedFrom((TDLLResultListItem *)this->head, offset);
    ftsAll = (float)getFts((TDLLResultListItem *)&pHead[idxAll]);
    ftsFrom = (float)getFte((TDLLResultListItem *)&pHead[idxFrom]);
    calcTailItemAndCheckPeak(this->pRawBagBuffer, (TDLLResultListItem *)this->head, idxAll, idxFrom, ftsAll,
                             ftsFrom);
    if (this->pIndexWrapper->setResultTypeCode((TDLLResultListItem *)this->head, offset))
    {
        this->pIndexWrapper->setIndexBuf();
    }
    this->calcResultUntilTailReached(pHead, offset, freq, offset, 0);
    this->pIndexWrapper->cleanIndexBuf();
    tail = this->pResInfWrapper->filterOutInvalidTr((TDLLResultListItem *)this->head, offset);
    if (tail >= 0)
    {
        if (pHead[0].TypeCode[1] == C80)
        {
            pHead[0].TypeCode[1] = C66;
        }
        if (pHead[tail].TypeCode[2] == C80)
        {
            pHead[tail].TypeCode[2] = C66;
        }
    }
    this->pResInfWrapper->calcW50etcAll((TDLLResultListItem *)this->head, tail, this->pRawBagBuffer, freq);
    return tail;
}

// sub_10003F8A
void CResultCaculator::calcAreaTimeHeight(int32_t *rbagBuffer)
{
    int32_t head;
    double area;
    int32_t timeStart;
    float hMaximin;
    int32_t tail;
    int32_t timeHead;
    int32_t timeTail;
    int32_t time;
    int32_t i;
    int32_t j;
    int32_t eWidth;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    IndexBuffer1 *pIndex = this->pIndexWrapper->indexBuf;

    if (!rbagBuffer)
    {
        rbagBuffer = this->pRawBagBuffer;
    }
    if (rbagBuffer && this->baseIdx > 0)
    {
        if (this->pEventManager->topOrBottom)
        {
            if ((int64_t)(this->head) > *(int32_t *)this->pResInfWrapper && pHead[0].ts < pHead[-1].te)
            {
                pHead[0].ts = pHead[-1].te;
                pHead[0].hs = pHead[-1].he;
                pHead[0].Areas = pHead[-1].Areae;
                pHead[0].fts = pHead[-1].fte;
            }
        }
        else
        {
            eWidth = 3 * this->pEventManager->eventWidth;
            for (i = 0; i < this->baseIdx; ++i)
            {
                if ((pHead[i].TypeCode[0] & X7F) != C78 && !this->isTrEqualTsV0(i) && (i <= 0 || !this->isTrEqualTeV0(i - 1)))
                {
                    timeStart = pHead[i].ts;
                    if (timeStart > this->pAnalExecutor->rawbagOffset - 1)
                    {
                        timeStart = this->pAnalExecutor->rawbagOffset - 1;
                    }
                    area = pHead[i].Areas;
                    timeHead = timeStart - eWidth;
                    timeTail = eWidth + timeStart;
                    if ((int32_t)(timeStart - eWidth) < 0)
                    {
                        timeHead = 0;
                    }
                    if (i > 0 && timeHead < pHead[i - 1].tr)
                    {
                        timeHead = (int32_t)pHead[i - 1].tr;
                    }
                    if (!i && (int64_t)pHead > *(int32_t *)this->pResInfWrapper && timeHead < pHead[-1].te)
                    {
                        timeHead = pHead[-1].te;
                    }
                    if (timeTail > this->pAnalExecutor->rawbagOffset - 1)
                    {
                        timeTail = this->pAnalExecutor->rawbagOffset - 1;
                    }
                    if (timeTail > pHead[i].tr)
                    {
                        timeTail = (int32_t)pHead[i].tr;
                    }
                    if (this->pEventManager->topOrBottom)
                    {
                        j = timeHead;
                        time = timeStart;
                        hMaximin = (float)rbagBuffer[timeStart];
                        while (j <= timeTail)
                        {
                            if ((float)rbagBuffer[j] > hMaximin)
                            {
                                hMaximin = (float)rbagBuffer[j];
                                time = j;
                            }
                            ++j;
                        }
                    }
                    else
                    {
                        j = timeHead;
                        time = timeStart;
                        hMaximin = (float)rbagBuffer[timeStart];
                        while (j <= timeTail)
                        {
                            if ((float)rbagBuffer[j] < hMaximin)
                            {
                                hMaximin = (float)rbagBuffer[j];
                                time = j;
                            }
                            ++j;
                        }
                    }
                    if (time >= timeStart)
                    {
                        if (time > timeStart)
                        {
                            for (j = timeStart + 1; j <= time; ++j)
                            {
                                area = (double)rbagBuffer[j] + area;
                            }
                        }
                    }
                    else
                    {
                        for (j = timeStart; j >= time + 1; --j)
                        {
                            area = area - (double)rbagBuffer[j];
                        }
                    }
                    if (time != timeStart)
                    {
                        pHead[i].Areas = area;
                        pHead[i].ts = time;
                        pHead[i].hs = hMaximin;
                        if (i > 0 && pHead[i - 1].te == timeStart)
                        {
                            pHead[i - 1].Areae = area;
                            pHead[i - 1].te = time;
                            pHead[i - 1].he = hMaximin;
                        }
                    }
                }
            }
            for (i = 0; i < this->pIndexWrapper->numValue; ++i)
            {
                head = pIndex[i].head;
                tail = pIndex[i].tail;
                if (head >= 0 && head < this->baseIdx && tail >= 0 && tail < this->baseIdx && tail >= head)
                {
                    pHead[head].te = pHead[tail].te;
                    pHead[head].he = pHead[tail].he;
                    pHead[head].Areae = pHead[tail].Areae;
                }
            }
        }
    }
}

// sub_1001A2A0
int32_t CResultCaculator::isTrEqualTsV0(int32_t idx)
{
    int32_t tr;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    tr = (int32_t)pHead[idx].tr;
    return tr && pHead[idx].ts == tr;
}

// sub_1001A240
int32_t CResultCaculator::isTrEqualTeV0(int32_t idx)
{
    int32_t tr;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    tr = (int32_t)pHead[idx].tr;
    return tr && pHead[idx].te == tr;
}

// sub_1000874E
void CResultCaculator::setHeightValues(int32_t *rbagBuffer)
{
    int32_t i;
    int32_t rbagTail;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (this->finishFlag)
    {
        if (!rbagBuffer)
        {
            rbagBuffer = this->pRawBagBuffer;
        }
        if (rbagBuffer)
        {
            rbagTail = this->pAnalExecutor->rawbagOffset - 1;
            for (i = 0;; ++i)
            {
                if (i >= this->baseIdx)
                {
                    break;
                }
                if (pHead[i].ts > rbagTail)
                {
                    pHead[i].ts = rbagTail;
                }
                if (pHead[i].te > rbagTail)
                {
                    pHead[i].te = rbagTail;
                }
                if (pHead[i].tr > rbagTail)
                {
                    pHead[i].tr = (float)rbagTail;
                }
                pHead[i].hs = (float)rbagBuffer[pHead[i].ts];
                pHead[i].he = (float)rbagBuffer[pHead[i].te];
                pHead[i].hr = (float)rbagBuffer[(int32_t)pHead[i].tr];
            }
        }
    }
}

// sub_100091B1
int32_t CResultCaculator::calcResultUntilTailReached(TDLLResultListItem *pRes, int32_t tail, float freq,
                                                     int32_t offset, int32_t flag)
{
    int32_t i;
    int32_t j;
    int32_t k;
    double summary;
    int32_t idxHead;
    int32_t tr;
    float tri;
    float fts;
    float fte;
    float ftsi;
    float hs;
    float hsi;
    float hr;
    float he;
    float eSlope;
    int32_t idxArray[128];
    int32_t *pIdxArray;
    double d1_60 = 1.0 / 60.0;

    IndexBuffer1 *pIndex = (IndexBuffer1 *)this->pIndexWrapper->head;

    pIdxArray = idxArray;
    if (tail < 0)
    {
        return false;
    }
    memset(idxArray, 0, sizeof(idxArray));
    calcTypeCodeFromBeginning(pRes, tail, idxArray);
    pIdxArray = idxArray;
    if (flag)
    {
        idxHead = -1;
    }
    else
    {
        idxHead = this->pIndexWrapper->getIndexHead();
    }
    for (i = 0; i <= tail; ++i)
    {
        if (pRes[i].ts == pRes[i].te)
        {
            pRes[i].tr = 0;
        }
        if (i)
        {
            if (i == *pIdxArray)
            {
                fts = (float)getFts(&pRes[*pIdxArray]);
                hs = pRes[*pIdxArray].hs;
                pIdxArray++;
                eSlope = (float)this->calcResultEndValue1(pRes, 0, tail, i, hs, fts, pIdxArray, offset);
            }
        }
        else
        {
            if ((void *)pRes == (void *)this->head)
            {
                this->calcResultEndValue0(pRes, 0, tail, 0, *pIdxArray);
            }
            if (idxHead || !pIndex->flag)
            {
                fts = (float)getFts(pRes);
                hs = pRes->hs;
                eSlope = (float)this->calcResultEndValue1(pRes, 0, tail, 0, hs, fts, pIdxArray, offset);
            }
            else
            {
                j = pIndex->tail;
                fts = (float)getFts(&pRes[j]);
                hs = pRes[j].hs;
                eSlope = (float)this->calcResultEndValue1(pRes, 0, tail, 0, hs, fts, pIdxArray, offset);
            }
        }
        if (idxHead >= 0 && i == idxHead)
        {
            i = this->calcResultItemValue(idxHead, offset, fts, hs, eSlope, freq);
            idxHead = this->pIndexWrapper->getIndexHead();
        }
        else
        {
            ftsi = (float)getFts(&pRes[i]);
            fte = (float)getFte(&pRes[i]);
            tri = pRes[i].tr;
            hsi = (float)(((double)ftsi - fts) * eSlope + hs);
            he = (float)(((double)fte - fts) * eSlope + hs);
            hr = (float)(((double)tri - fts) * eSlope + hs);
            pRes[i].hsv = (float)calcHsv(&pRes[i], fts, hs, eSlope);
            pRes[i].hev = (float)calcHev(&pRes[i], fts, hs, eSlope);
            pRes[i].RTime = (float)(d1_60 * pRes[i].tr / freq);
            pRes[i].Area = pRes[i].Areae - pRes[i].Areas;
            if ((pRes[i].TypeCode[0] & X7F) == C78)
            {
                pRes[i].Height = (float)((double)(int32_t)hr - pRes[i].hr);
            }
            else
            {
                pRes[i].Height = (float)(pRes[i].hr - (double)(int32_t)hr);
            }
            if ((pRes[i].TypeCode[0] & X7F) == C78)
            {
                if (flag || pRes[i].hr <= (double)pRes[i].hs || ((double)hsi + he) * 0.5 * ((double)fte - ftsi) >= pRes[i].Area)
                {
                    pRes[i].Area =
                        (((double)hsi + he) * 0.5 * ((double)fte - ftsi) - pRes[i].Area) / freq;
                }
                else
                {
                    summary = 0.0;
                    for (j = pRes[i].ts + 1; j <= pRes[i].te; ++j)
                    {
                        summary = this->pRawBagBuffer[j] + summary;
                    }
                    pRes[i].Areae = summary - pRes[i].Area + pRes[i].Areae;
                    pRes[i].Area = (((double)hsi + he) * 0.5 * ((double)fte - ftsi) - summary) / freq;
                }
            }
            else if (flag || pRes[i].hr <= (double)pRes[i].hs || ((double)hsi + he) * 0.5 * ((double)fte - ftsi) <= pRes[i].Area)
            {
                pRes[i].Area = (pRes[i].Area - ((double)hsi + he) * 0.5 * ((double)fte - ftsi)) / freq;
            }
            else
            {
                summary = 0.0;
                for (k = pRes[i].ts + 1; k <= pRes[i].te; ++k)
                {
                    summary = this->pRawBagBuffer[k] + summary;
                }
                pRes[i].Areae = summary - pRes[i].Area + pRes[i].Areae;
                pRes[i].Area = (summary - ((double)hsi + he) * 0.5 * ((double)fte - ftsi)) / freq;
            }
            if ((pRes[i].TypeCode[0] & X7F) == C78)
            {
                pRes[i].ALeft =
                    (((double)hr + hsi) * 0.5 * ((double)tri - ftsi) - (pRes[i].Arear - pRes[i].Areas)) / freq;
            }
            else
            {
                pRes[i].ALeft =
                    (pRes[i].Arear - pRes[i].Areas - ((double)hr + hsi) * 0.5 * ((double)tri - ftsi)) / freq;
            }
            pRes[i].ARight = pRes[i].Area - pRes[i].ALeft;
            pRes[i].Area = d1_60 * this->xMinUnitK * pRes[i].Area;
            pRes[i].ALeft = d1_60 * this->xMinUnitK * pRes[i].ALeft;
            pRes[i].ARight = d1_60 * this->xMinUnitK * pRes[i].ARight;
        }
        if (pRes[i].TypeCode[0] == C32 || !pRes[i].TypeCode[0])
        {
            tr = (int32_t)pRes[i].tr;
            if (tr == pRes[i].te)
            {
                pRes[i].TypeCode[0] = C70;
            }
            else if (tr == pRes[i].ts)
            {
                pRes[i].TypeCode[0] = C66;
            }
        }
    }
    if (pRes[tail].TypeCode[2] == C86)
    {
        pRes[tail].TypeCode[2] = C66;
    }
    return true;
}

// sub_1000B02B
double CResultCaculator::calcResultEndValue1(TDLLResultListItem *pRes, int32_t head, int32_t tail, int32_t idx,
                                             float hs, float fts, int32_t *idxArray, int32_t offset)
{
    float fte;
    int32_t i;
    float result;

    if (pRes == this->head && tail == offset && (pRes[tail].TypeCode[0] & X7F) == C84)
    {
        tail = searchSucceedFrom(pRes, tail);
    }
    result = 0.0;
    if (idxArray[0] <= 0)
    {
        if ((pRes[idx].TypeCode[0] & X7F) == C83 && pRes[idx].TypeCode[2] == C66)
        {
            fte = (float)getFte(&pRes[idx]);
            if (fte != fts)
            {
                result = (pRes[idx].he - hs) / (fte - fts);
            }
        }
        else
        {
            fte = (float)getFte(&pRes[tail]);
            if ((double)pRes[tail].te != fts)
            {
                result = (pRes[tail].he - hs) / (fte - fts);
            }
        }
    }
    else
    {
        i = idxArray[0] - 1;
        fte = (float)getFte(&pRes[head + i]);
        if (fte != fts)
        {
            result = (pRes[head + i].he - hs) / (fte - fts);
        }
        this->calcResultEndValue0(pRes, head, tail, idxArray[0], idxArray[1]);
    }
    return result;
}

// sub_1000AE05
int32_t CResultCaculator::calcResultEndValue0(TDLLResultListItem *pRes, int32_t head, int32_t tail, int32_t idx0,
                                              int32_t idx1)
{
    float fte;
    float fts;
    int32_t i;
    int32_t j;

    i = head + idx0;
    if (idx1 > 0)
    {
        j = head + idx1 - 1;
    }
    else
    {
        j = tail;
    }
    if ((pRes[i].TypeCode[0] & X7F) == C116)
    {
        i = searchSucceedTypeCodeIdx(&pRes[i], i, tail);
    }
    if ((pRes[j].TypeCode[0] & X7F) == C84)
    {
        j = searchSucceedTypeCodeIdx(&pRes[j], j, tail);
    }
    fts = (float)getFts(&pRes[i]);
    fte = (float)getFte(&pRes[j]);
    if (pRes[i].hs < pRes[j].he)
    {
        if (checkPeakSlope(this->pRawBagBuffer, pRes, i, j, fts, fte))
        {
            if (pRes[i].tr > 0)
            {
                pRes[i].TypeCode[1] = C66;
                if (i - 1 >= 0)
                {
                    pRes[i - 1].TypeCode[2] = C66;
                }
            }
            return true;
        }
        return false;
    }
    if (pRes[i].hs <= pRes[j].he || !calcTailResultItemValue(this->pRawBagBuffer, pRes, i, j, fts, fte))
    {
        return false;
    }
    if (pRes[i].tr > 0)
    {
        pRes[j].TypeCode[2] = C66;
        if (j < tail)
        {
            pRes[j + 1].TypeCode[1] = C66;
        }
    }
    return true;
}

// sub_10009D44
int32_t CResultCaculator::calcResultItemValue(int32_t head, int32_t offset, float fts, float hs, float slope,
                                              float freq)
{
    int32_t result;
    double hsv_hev;
    float vfte;
    float vhs_e;
    float vfts;
    float vtr;
    float vhs_s;
    int32_t i;
    int32_t j;
    int32_t idx;
    float vhs_r;
    int32_t tail;
    int32_t head1;
    int32_t tail1;
    int32_t head2;
    double d1_60 = 1.0 / 60.0;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (head < 0 || offset < 0)
    {
        return head;
    }
    head2 = this->pIndexWrapper->indexBuf->flag;
    tail = -1;
    head1 = -1;
    tail1 = -1;
    this->pIndexWrapper->getHeadTail1(&head, &tail, &head1, &tail1);
    for (i = head; i <= tail; ++i)
    {
        if (pHead[i].ts == pHead[i].te)
        {
            pHead[i].tr = 0;
        }
        if (head1 >= 0 && i == head1)
        {
            vfte = (float)getFte(&pHead[tail1]);
            vfts = (float)getFts(&pHead[head1]);
            calcTailItemAndCheckPeak(this->pRawBagBuffer, pHead, head1, tail1, vfts, vfte);
            if (this->calcResultUntilTailReached(&pHead[head1], tail1 - head1, freq, offset, 1))
            {
                i = tail1;
                this->pIndexWrapper->getHeadTail2(&head1, &tail1);
            }
        }
        else
        {
            vfts = (float)getFts(&pHead[i]);
            vfte = (float)getFte(&pHead[i]);
            vtr = pHead[i].tr;
            vhs_s = (vfts - fts) * slope + hs;
            vhs_e = (vfte - fts) * slope + hs;
            vhs_r = (vtr - fts) * slope + hs;
            if (i == head && head2)
            {
                hsv_hev = calcHeadHsv(&pHead[i], fts, hs, slope, vfts, pHead[i].hs);
                pHead[i].hsv = (float)hsv_hev;
            }
            else
            {
                hsv_hev = calcHsv(&pHead[i], fts, hs, slope);
                pHead[i].hsv = (float)hsv_hev;
            }
            if (i != head || head2)
            {
                hsv_hev = calcHev(&pHead[i], fts, hs, slope);
                pHead[i].hev = (float)hsv_hev;
            }
            else
            {
                hsv_hev = calcHeadHev(&pHead[i], fts, hs, slope, vfte, pHead[i].he);
                pHead[i].hev = (float)hsv_hev;
            }
            if ((int32_t)vtr)
            {
                pHead[i].RTime = (float)(pHead[i].tr / (freq * 60.0));
                pHead[i].Height = (float)(pHead[i].hr - (double)(int32_t)vhs_r);
                if (head2)
                {
                    pHead[i].ALeft =
                        (pHead[i].Arear - pHead[i].Areas - ((double)vhs_r + vhs_s) * 0.5 * (vtr - vfts)) / freq;
                    if (!isPeakType36_83(&pHead[i]))
                    {
                        pHead[i].ARight =
                            (pHead[i].Areae - pHead[i].Arear - ((double)vhs_r + vhs_e) * 0.5 * (vfte - vtr)) / freq;
                    }
                }
                else
                {
                    if (!isPeakType36_115(&pHead[i]))
                    {
                        pHead[i].ALeft =
                            (pHead[i].Arear - pHead[i].Areas - ((double)vhs_r + vhs_s) * 0.5 * (vtr - vfts)) / freq;
                    }
                    pHead[i].ARight =
                        (pHead[i].Areae - pHead[i].Arear - ((double)vhs_r + vhs_e) * 0.5 * (vfte - vtr)) / freq;
                }
                pHead[i].ALeft = d1_60 * this->xMinUnitK * pHead[i].ALeft;
                pHead[i].ARight = d1_60 * this->xMinUnitK * pHead[i].ARight;
            }
            else
            {
                pHead[i].RTime = 0;
                pHead[i].Height = 0;
                pHead[i].Area = 0;
                if (i < offset && !head2)
                {
                    if (pHead[i].TypeCode[2] == C80)
                    {
                        pHead[i + 1].TypeCode[1] = C66;
                        pHead[i].TypeCode[2] = C66;
                        pHead[head].TypeCode[2] = C66;
                    }
                    else if (i > 0 && i == tail)
                    {
                        if (pHead[i - 1].TypeCode[2] == C80)
                        {
                            pHead[i + 1].TypeCode[1] = C66;
                            pHead[i - 1].TypeCode[2] = C66;
                            pHead[head].TypeCode[2] = C66;
                        }
                        else
                        {
                            pHead[head].TypeCode[2] = pHead[i + 1].TypeCode[1];
                        }
                    }
                }
            }
        }
    }
    if (head2)
    {
        idx = tail;
    }
    else
    {
        idx = head;
    }
    for (j = head; j <= tail; ++j)
    {
        if (j != idx)
        {
            if (head2)
            {
                pHead[idx].ALeft = pHead[idx].ALeft - pHead[j].Area;
            }
            else
            {
                pHead[idx].ARight = pHead[idx].ARight - pHead[j].Area;
            }
        }
    }
    pHead[idx].Area = pHead[idx].ALeft + pHead[idx].ARight;
    if (!head)
    {
        pHead[0].TypeCode[1] = C66;
        if (head2)
        {
            pHead[idx].TypeCode[1] = C66;
        }
    }
    if (!head2 && tail == offset)
    {
        pHead[tail].TypeCode[2] = C66;
        if (!head2)
        {
            pHead[idx].TypeCode[2] = C66;
        }
    }
    if (head2 && isTypeCode36(&pHead[idx]))
    {
        result = tail - 1;
    }
    else
    {
        result = tail;
    }
    return result;
}

// sub_10004F5E
void CResultCaculator::calcTypeCode(int32_t offset)
{
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    this->typeCodeStatus = true;
    this->tickmarkStatus = false;
    pHead[idx].TypeCode[0] = C32;
    if (idx)
    {
        if (pHead[idx - 1].te == (int32_t)pHead[idx - 1].tr)
        {
            this->pCTickmarkManager->setFirstTickmark((int32_t)pHead[idx - 1].tr, APEX_TICK);
            pHead[idx - 1].TypeCode[0] = C70;
            pHead[idx - 1].TypeCode[2] = C88;
            pHead[idx].TypeCode[1] = C88;
            this->isAreaShiftPositive(idx - 1, false);
        }
        else
        {
            if (pHead[idx - 1].TypeCode[2] != C66)
            {
                pHead[idx - 1].TypeCode[2] = C86;
            }
            if ((pHead[idx - 1].TypeCode[0] & X7F) == C83 || (pHead[idx - 1].tr - pHead[idx - 1].ts) * 4.0 < pHead[idx - 1].te - pHead[idx - 1].tr)
            {
                this->isAreaShiftPositive(idx - 1, true);
            }
            this->pCTickmarkManager->setFirstTickmark(pHead[idx].ts, VALLEY_TICK);
            if (this->pIndexWrapper->isIndex2Valid())
            {
                pHead[idx].TypeCode[0] = C84;
            }
            pHead[idx].TypeCode[1] = C86;
        }
    }
    else
    {
        this->pCTickmarkManager->setFirstTickmark(pHead[0].ts, START_TICK);
        pHead[0].TypeCode[1] = C66;
    }
}

// sub_1000462F
int32_t CResultCaculator::finishEndValueAccordingBuffer10(int32_t buffNo, int32_t tail, int32_t offset)
{
    double areaN;
    int32_t teN;
    int32_t heN;
    int32_t timeN;
    int32_t tailN;
    int32_t i;
    int32_t j;
    int32_t eventWidth;
    int32_t count;

    timeN = this->pBuffer10Wrapper->getBuffer10Time(buffNo);
    heN = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(buffNo);
    areaN = this->pBuffer10Wrapper->getBuffer10Area(buffNo);
    eventWidth = this->pEventManager->eventWidth;
    count = timeN - eventWidth;
    if (tail)
    {
        tailN = tail;
    }
    else
    {
        tailN = eventWidth + timeN;
    }
    if (count < 0)
    {
        count = 0;
    }
    if ((int32_t)this->pTDLLItem[offset].tr > 0 && count < this->pTDLLItem[offset].tr)
    {
        count = (int32_t)this->pTDLLItem[offset].tr;
    }
    if (tailN > this->pAnalExecutor->rawbagOffset - 1)
    {
        tailN = this->pAnalExecutor->rawbagOffset - 1;
    }
    teN = timeN;
    while (count <= tailN)
    {
        if (this->pEventManager->topOrBottom)
        {
            if (this->pRawBagBuffer[count] > heN)
            {
                heN = this->pRawBagBuffer[count];
                teN = count;
            }
        }
        else if (this->pRawBagBuffer[count] < heN)
        {
            heN = this->pRawBagBuffer[count];
            teN = count;
        }
        ++count;
    }
    if (teN <= timeN)
    {
        if (teN < timeN)
        {
            for (i = teN + 1; i <= timeN; ++i)
            {
                areaN = areaN - (double)this->pRawBagBuffer[i];
            }
        }
    }
    else
    {
        for (j = timeN + 1; j <= teN; ++j)
        {
            areaN = (double)this->pRawBagBuffer[j] + areaN;
        }
    }
    this->pTDLLItem[offset].Areae = areaN;
    this->pTDLLItem[offset].te = teN;
    this->pTDLLItem[offset].he = (float)heN;
    return this->checkTailReached(offset);
}

// sub_100044FE
int32_t CResultCaculator::checkTailReached(int32_t offset)
{
    int32_t tail;
    int32_t head;
    IndexBuffer1 *pIndex = (IndexBuffer1 *)this->pIndexWrapper->head;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if ((int32_t)pHead[offset].tr)
    {
        return false;
    }
    if (this->pIndexWrapper->len <= 0)
    {
        return false;
    }
    if (this->isIndex1Valid_())
    {
        return false;
    }
    pIndex += this->pIndexWrapper->len - 1;
    if (pIndex->tail <= 0 || offset + this->baseIdx != pIndex->tail)
    {
        return false;
    }
    head = pIndex->head;
    tail = offset + this->baseIdx;
    pHead[head].te = pHead[tail].te;
    pHead[head].he = pHead[tail].he;
    pHead[head].Areae = pHead[tail].Areae;
    return true;
}

// sub_1001A300
int32_t CResultCaculator::isIndex1Valid_()
{
    return this->pIndexWrapper->isIndex1Valid();
}

// sub_1001A490
int32_t CResultCaculator::isTHValueInitialized(int32_t idx)
{
    return !(int32_t)this->pTDLLItem[idx].tr && !(int32_t)this->pTDLLItem[idx].hr && this->pTDLLItem[idx].ts && this->pTDLLItem[idx].te;
}

// sub_1001B300
int32_t CResultCaculator::isDriftOrSlopeExceedDeltaH()
{
    int32_t driftFlag;
    int32_t slopeX10Falg;
    int32_t scaledSlopeFlag;

    driftFlag = this->isEventDriftExceed();
    scaledSlopeFlag = this->isScaledSlopeExceed(15);
    slopeX10Falg = this->isSlopeX10Exceed(0);
    return driftFlag || scaledSlopeFlag || slopeX10Falg;
}

// sub_1001B360
int32_t CResultCaculator::isEventDriftExceed()
{
    float deltaT;
    float deltaH;

    if ((this->pTDLLItem[0].TypeCode[0] & X7F) == C83 || fabs(this->pTDLLItem->hr - this->pTDLLItem->hs) > 10000.0)
    {
        return false;
    }
    deltaT = (float)(getFte(this->pTDLLItem) - this->pTDLLItem->tr);
    if (deltaT <= 0.0)
    {
        return false;
    }
    deltaH = (this->pTDLLItem->hr - this->pTDLLItem->he) / deltaT;
    if (this->pEventManager->topOrBottom)
    {
        deltaH = (float)(0.0 - deltaH);
    }
    return this->pEventManager->getEventDrift() > deltaH;
}

// sub_10008058
int32_t CResultCaculator::isScaledSlopeExceed(int32_t scale)
{
    int32_t result;
    float deltaH;
    float scaledSlope;

    if ((this->pTDLLItem->TypeCode[0] & X7F) == C83 || fabs(*(float *)((BYTE *)this->pTDLLItem + 168) - *(float *)((BYTE *)this->pTDLLItem + 172)) > 10000.0)
    {
        return false;
    }
    deltaH = this->pTDLLItem->hr - this->pTDLLItem->he;
    scaledSlope = this->pEventManager->getNormalizeSlope();
    scaledSlope = (float)((double)scale * scaledSlope);
    if (this->pEventManager->topOrBottom)
    {
        deltaH = (float)(0.0 - deltaH);
    }
    if (this->pTDLLItem->te <= (int32_t)this->pTDLLItem->tr || deltaH > (double)scaledSlope)
    {
        return false;
    }
    deltaH = this->pTDLLItem->hr - this->pTDLLItem->hs;
    result = false;
    if (deltaH > 0.0)
    {
        if (4.0 * scaledSlope > deltaH)
        {
            result = true;
        }
    }
    return result;
}

// sub_10003A52
int32_t CResultCaculator::isSlopeX10Exceed(int32_t offset)
{
    double slopeM10;
    double deltaH;

    slopeM10 = this->pEventManager->getSlopeX10();
    deltaH = this->getDeltaHr(offset);
    if (this->pEventManager->topOrBottom)
    {
        deltaH = 0.0 - deltaH;
    }
    return deltaH <= slopeM10;
}

// sub_1000382C
double CResultCaculator::getDeltaHr(int32_t offset)
{
    float slope;
    float diffH;
    float fte;
    float hrFromTr;
    float deltaHr;
    TDLLResultListItem *pHead = this->pTDLLItem;
    pHead = &pHead[offset];

    deltaHr = 0.0;
    if (!pHead->ts)
    {
        return fabs(pHead->hr - pHead->hs);
    }
    if ((int32_t)pHead->tr == pHead->ts)
    {
        return fabs(pHead->hr - pHead->he);
    }
    if ((int32_t)pHead->tr == pHead->te || pHead->te == pHead->ts)
    {
        return fabs(pHead->hr - pHead->hs);
    }
    fte = (float)getFte(pHead);
    if (getFts(pHead) != fte)
    {
        diffH = pHead->he - pHead->hs;
        fte = (float)getFte(pHead);
        slope = (float)(diffH / (fte - getFts(pHead)));
        hrFromTr = (float)((pHead->tr - getFts(pHead)) * slope + pHead->hs);
        deltaHr = pHead->hr - hrFromTr;
    }
    return deltaHr;
}

// sub_10003C55
int32_t CResultCaculator::goPrevAfterClear()
{
    if (this->baseIdx <= 0)
    {
        return false;
    }
    memset(this->pTDLLItem, 0, sizeof(TDLLResultListItem));
    --this->baseIdx;
    this->pTDLLItem--;
    return true;
}

// sub_1000AA11
int32_t CResultCaculator::calcResultNotC86(int32_t head, int32_t tail)
{
    float freq;
    int32_t *pNum;
    int32_t j;
    int32_t calcIdx;
    int32_t i;
    int32_t remain;
    TDLLResultListItem *pRes;

    remain = 0;
    pRes = (TDLLResultListItem *)this->pResInfWrapper->getHead();
    pNum = this->pResInfWrapper->getNumPointer();
    do
    {
        if (head <= 0)
        {
            break;
        }
        if (pRes[head].TypeCode[1] != C86)
        {
            break;
        }
        --head;
    } while (!typeCode1Without84_116(&pRes[head], 3));
    do
    {
        if (tail >= *pNum - 1)
        {
            break;
        }
        if (pRes[tail].TypeCode[2] != C86)
        {
            break;
        }
        ++tail;
    } while (!typeCode2Without84_116(&pRes[tail], 3));
    for (i = head; i <= tail; ++i)
    {
        if (i == head || typeCode1Without84_116(&pRes[i], 3))
        {
            this->head = (void *)&pRes[i];
            for (j = 0;
                 j < tail - i && (!typeCode1Without84_116(&pRes[i + j + 1], 3) || isPeakType36_115(&pRes[i + j + 1]));
                 ++j)
            {
                ;
            }
            this->setIndexBufUntil(j);
            freq = (float)this->pEventManager->getRawbagFreqValue();
            calcIdx = this->calcResultValue(j, freq);
            if (calcIdx != j)
            {
                tail -= j - calcIdx;
                *pNum -= j - calcIdx;
                remain = j - calcIdx;
            }
            i += calcIdx;
        }
    }
    return remain;
}

// sub_1000A64C
void CResultCaculator::setIndexBufUntil(int32_t tail)
{
    int32_t j;
    int32_t l;
    int32_t i;
    int32_t k;
    TDLLResultListItem *pRes = (TDLLResultListItem *)this->head;
    for (i = 0; i <= tail; ++i)
    {
        if (isPeakType36_83(&pRes[i]) && i < tail && (pRes[i + 1].TypeCode[0] & X7F) == C84 && typeCode1Without84_116(&pRes[i + 1], 0) && this->setIndexBuf1Head(i, 0))
        {
            do
            {
                if (this->setIndexBuf2Head(++i, 0))
                {
                    for (j = i; j <= tail; ++j)
                    {
                        if ((pRes[j].TypeCode[0] & X7F) == C84 && typeCode2Without84_116(&pRes[j], 0))
                        {
                            this->setIndexBuf2Tail(j, 0);
                            break;
                        }
                    }
                    i = j;
                }
            } while (i < tail && (pRes[i + 1].TypeCode[0] & X7F) == C84 && typeCode1Without84_116(&pRes[i + 1], 0));
            this->setIndexBuf1Tail(i, false);
        }
    }
    for (k = tail; k >= 0; --k)
    {
        if (isPeakType36_115(&pRes[k]) && k > 0 && (pRes[k - 1].TypeCode[0] & X7F) == C116 && typeCode2Without84_116(&pRes[k - 1], 0) && this->setIndexBuf1Head(k, 1))
        {
            do
            {
                if (this->setIndexBuf2Head(--k, 1))
                {
                    for (l = k; l >= 0; --l)
                    {
                        if ((pRes[l].TypeCode[0] & X7F) == C116 && typeCode1Without84_116(&pRes[l], 0))
                        {
                            this->setIndexBuf2Tail(l, 1);
                            break;
                        }
                    }
                    k = l;
                }
            } while (k > 0 && (pRes[k - 1].TypeCode[0] & X7F) == C116 && typeCode2Without84_116(&pRes[k - 1], 0));
            this->setIndexBuf1Tail(k, true);
        }
    }
}

// sub_10005A76
int32_t CResultCaculator::setIndexBuf1Head(int32_t head, int32_t flag)
{
    int32_t len;
    int32_t result;
    char *errMsg;
    int32_t *pNumber;

    pNumber = this->pIndexWrapper->getNumPointer();
    if (!*pNumber)
    {
        this->pIndexWrapper->initHead();
    }
    if (*pNumber < this->pIndexWrapper->getLen())
    {
        if (this->pIndexWrapper->createIndexBuf2())
        {
            ++*pNumber;
            this->pIndexWrapper->currentTail2 = 0;
            this->pIndexWrapper->indexBuf->head = head;
            this->pIndexWrapper->indexBuf->pIndex2 = this->pIndexWrapper->indexBuf20;
            this->pIndexWrapper->indexBuf->flag = flag;
            result = true;
        }
        else
        {
            if (gLanguage == LANG_EN)
            {
                int32_t len = MIN(sizeof(this->pAnalInfo->errMsg), sizeof(aNoEnoughMemory0_EN));
                strncpy(this->pAnalInfo->errMsg, aNoEnoughMemory0_EN, len);
            }
            else
            {
                int32_t len = MIN(sizeof(this->pAnalInfo->errMsg), sizeof(aNoEnoughMemory0_CN));
                strncpy(this->pAnalInfo->errMsg, aNoEnoughMemory0_CN, len);
            }
            this->pAnalInfo->code = ANAL_ERROR;
            result = false;
        }
    }
    else
    {
        if (gLanguage == LANG_EN)
        {
            errMsg = aNumberOfSolven0_EN;
        }
        else
        {
            errMsg = (char *)&aNumberOfSolven0_CN;
        }
        len = this->pIndexWrapper->getLen();
        errMsg = formatExceedError(errMsg, len);
        strncpy(this->pAnalInfo->errMsg, errMsg, len + 1);
        this->pAnalInfo->code = ANAL_ERROR;
        result = false;
    }
    return result;
}

// sub_10005C6A
int32_t CResultCaculator::setIndexBuf2Head(int32_t head, int32_t flag)
{
    char *errMsg;
    int32_t result;

    if (this->pIndexWrapper->currentTail2 < this->pIndexWrapper->indexCount2)
    {
        ++this->pIndexWrapper->currentTail2;
        this->pIndexWrapper->indexBuf2->head2 = head;
        this->pIndexWrapper->indexBuf2->flag2 = flag;
        result = true;
    }
    else
    {
        if (gLanguage == LANG_EN)
        {
            errMsg = formatExceedError(aNumberOfTangen0_EN, this->pIndexWrapper->indexCount2);
        }
        else
        {
            errMsg = formatExceedError(aNumberOfTangen0_CN, this->pIndexWrapper->indexCount2);
        }
        // strncpy(this->pAnalInfo->errMsg, errMsg, sizeof(this->pAnalInfo->errMsg));
        snprintf(this->pAnalInfo->errMsg, sizeof(this->pAnalInfo->errMsg), "%s", errMsg);
        this->pAnalInfo->code = ANAL_ERROR;
        result = false;
    }
    return result;
}

// sub_10005D19
int32_t CResultCaculator::setIndexBuf2Tail(int32_t tail, int32_t flag)
{
    this->pIndexWrapper->indexBuf2->tail2 = tail;
    this->pIndexWrapper->indexBuf2->flag2 = flag;
    this->pIndexWrapper->indexBuf2++;
    this->pIndexWrapper->indexBuf2->tail2 = -1;
    this->pIndexWrapper->indexBuf2->head2 = -1;
    this->pIndexWrapper->indexBuf2->flag2 = false;
    return true;
}

// sub_10005BB4
int32_t CResultCaculator::setIndexBuf1Tail(int32_t tail, int32_t flag)
{
    IndexBuffer1 *pIndex1 = this->pIndexWrapper->indexBuf;

    pIndex1->tail = tail;
    pIndex1->itemCount = this->pIndexWrapper->currentTail2;
    pIndex1->flag = flag;
    pIndex1++;
    pIndex1->head = -1;
    pIndex1->tail = -1;
    pIndex1->itemCount = 0;
    pIndex1->flag = false;
    pIndex1->pIndex2 = NULL;
    this->pIndexWrapper->index1Flag = 0;
    return true;
}

// sub_1000B2DA
int32_t CResultCaculator::calcIndex1ResultEndValue()
{
    int32_t idx;
    if (!this->pIndexWrapper->index1Flag)
    {
        return false;
    }
    if (this->pIndexWrapper->currentTail2 <= 0)
    {
        this->setSOL_TICK();
    }
    else if (this->pIndexWrapper->isIndex2Valid())
    {
        idx = this->pIndexWrapper->indexBuf->head;
        if (this->pIndexWrapper->indexBuf2)
        {
            idx = this->pIndexWrapper->indexBuf2->head2 - 1;
        }
        this->setIndex1EndResult(idx - this->baseIdx);
    }
    else if (this->isTHValueInitialized(-1))
    {
        this->setIndex1EndResult(-1);
    }
    else
    {
        this->setIndex1EndResult(0);
    }
    this->setBaseTailTangant(false);
    return true;
}

// sub_1001B4F0
int32_t CResultCaculator::setMinimumHS(int32_t *hsMin)
{
    int32_t result;
    int32_t i;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    if (this->baseIdx || pHead->ts)
    {
        *hsMin = (int32_t)pHead->hs;
        for (i = 0; i <= this->baseIdx; ++i)
        {
            if ((double)pHead[i].hs < *hsMin)
            {
                *hsMin = (int32_t)pHead[i].hs;
            }
        }
        result = true;
    }
    else
    {
        *hsMin = 0;
        result = false;
    }
    return result;
}

// sub_1001AAF0
CResultCaculator *CResultCaculator::setBaseTailTangant(int32_t baseTail)
{
    this->pIndexWrapper->baseTail = baseTail;
    return this;
}

// sub_10003B57
int32_t CResultCaculator::movePointerNext()
{
    int32_t len;
    char *errMsg;
    int32_t result;
    char *Source;

    if (this->isOffsetOutBoundV2(this->baseIdx))
    {
        if (gLanguage == LANG_EN)
        {
            Source = aNumberOfPeaksO_EN;
        }
        else
        {
            Source = (char *)&aNumberOfPeaksO3_CN;
        }
        len = this->pResInfWrapper->getLen();
        errMsg = formatExceedError(Source, len);
        // strncpy(this->pAnalInfo->errMsg, errMsg, sizeof(this->pAnalInfo->errMsg));
        snprintf(this->pAnalInfo->errMsg, sizeof(this->pAnalInfo->errMsg), "%s", errMsg);
        this->pAnalInfo->code = ANAL_ERROR;
        result = false;
    }
    else
    {
        this->pTDLLItem++;
        ++this->baseIdx;
        memset((void *)this->pTDLLItem, 0, sizeof(TDLLResultListItem));
        result = true;
    }
    return result;
}

// sub_10003F11
CResultCaculator *CResultCaculator::setStartFromBuffer10(int32_t buffNo, int32_t offset)
{
    double height;
    this->pTDLLItem[offset].Areas = this->pBuffer10Wrapper->getBuffer10Area(buffNo);
    this->pTDLLItem[offset].ts = this->pBuffer10Wrapper->getBuffer10Time(buffNo);
    height = this->pBuffer10Wrapper->getBuffer10Height(buffNo);
    this->pTDLLItem[offset].hs = (float)height;
    return this;
}

// sub_10004EE2
int32_t CResultCaculator::setStartAsPrevEnd()
{
    if (!this->isIndexPositive())
    {
        return false;
    }
    this->pTDLLItem[0].ts = this->pTDLLItem[-1].te;
    this->pTDLLItem[0].hs = this->pTDLLItem[-1].he;
    this->pTDLLItem[0].Areas = this->pTDLLItem[-1].Areae;
    this->pTDLLItem[0].fts = this->pTDLLItem[-1].fte;
    return true;
}

// sub_1000518A
int32_t CResultCaculator::setFirstApexTick(int32_t offset)
{
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    this->tickmarkStatus = true;
    this->typeCodeStatus = false;
    if (pHead[idx].ts == (int32_t)pHead[idx].tr)
    {
        if (this->pIndexWrapper->isIndex2Valid())
        {
            pHead[idx].TypeCode[0] = C84;
        }
        pHead[idx].TypeCode[0] = C66;
        pHead[idx - 1].TypeCode[2] = C88;
        pHead[idx].TypeCode[1] = C88;
        if (!pHead[idx].TypeCode[0])
        {
            pHead[idx].TypeCode[0] = C32;
        }
        this->isAreaShiftPositive(idx - 1, true);
    }
    else
    {
        this->isAreaShiftPositive(idx, false);
    }
    return this->pCTickmarkManager->setFirstTickmark((int32_t)pHead[idx].tr, APEX_TICK);
}

// sub_100053F9
int32_t CResultCaculator::isIndexBuf1EqualHead(int32_t offset)
{
    int32_t result;
    int32_t len;
    char *errMsg;
    char *format;
    int32_t head;
    int32_t idx;
    int32_t *pNum;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    pNum = this->pIndexWrapper->getNumPointer();
    if (*pNum)
    {
        if (this->pIndexWrapper->isIndex1Valid())
        {
            head = this->pIndexWrapper->indexBuf->head;
            if (idx == head)
            {
                return false;
            }
            if (idx == head + 1 && (int32_t)this->pTDLLItem[0].hr <= (int32_t)pHead[head].hr)
            {
                return false;
            }
            this->setIndex1EndResult(offset - 1);
        }
    }
    else
    {
        this->pIndexWrapper->initHead();
    }
    if (*pNum < this->pResInfWrapper->getLen())
    {
        if (this->pIndexWrapper->createIndexBuf2())
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[idx].ts, SOL_TICK);
            pHead[idx].TypeCode[0] = C83;
            ++*pNum;
            this->pIndexWrapper->indexBuf->head = idx;
            this->pIndexWrapper->indexBuf->pIndex2 = this->pIndexWrapper->indexBuf20;
            this->pIndexWrapper->currentTail2 = 0;
            this->pIndexWrapper->index1Flag = (offset != 0) + 1;
            result = true;
        }
        else
        {
            if (gLanguage == LANG_EN)
            {
                int32_t len = MIN(sizeof(this->pAnalInfo->errMsg), sizeof(aNoEnoughMemory_EN));
                strncpy((char *)this->pAnalInfo->errMsg, aNoEnoughMemory_EN, len);
            }
            else
            {
                int32_t len = MIN(sizeof(this->pAnalInfo->errMsg), sizeof(aNoEnoughMemory_CN));
                strncpy((char *)this->pAnalInfo->errMsg, aNoEnoughMemory_CN, len);
            }
            this->pAnalInfo->code = ANAL_ERROR;
            result = false;
        }
    }
    else
    {
        if (gLanguage == LANG_EN)
        {
            format = aNumberOfSolven_EN;
        }
        else
        {
            format = (char *)&aNumberOfPeaksO2_CN;
        }
        len = this->pResInfWrapper->getLen();
        errMsg = formatExceedError(format, len);
        // strncpy((char *)this->pAnalInfo->errMsg, errMsg, sizeof(this->pAnalInfo->errMsg));
        snprintf(this->pAnalInfo->errMsg, sizeof(this->pAnalInfo->errMsg), "%s", errMsg);
        this->pAnalInfo->code = ANAL_ERROR;
        result = false;
    }
    return result;
}

// sub_10005838
int32_t CResultCaculator::setPeakTB(int32_t offset)
{
    int32_t result;
    char *errMsg;
    int32_t idx;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    idx = offset + this->baseIdx;
    if (this->pIndexWrapper->currentTail2 > 0 && idx == this->pIndexWrapper->indexBuf2->head2)
    {
        return false;
    }
    if (this->pIndexWrapper->currentTail2 < this->pIndexWrapper->indexCount2)
    {
        this->pCTickmarkManager->setFirstTickmark(pHead[idx].ts, TAN_START_TICK);
        ++this->pIndexWrapper->currentTail2;
        this->pIndexWrapper->indexBuf2->head2 = idx;
        pHead[idx].TypeCode[0] = C84;
        pHead[idx].TypeCode[1] = C66;
        this->pIndexWrapper->index2Flag = 0;
        result = true;
    }
    else
    {
        if (gLanguage == LANG_EN)
        {
            errMsg = formatExceedError(aNumberOfTangen_EN, this->pIndexWrapper->indexCount2);
        }
        else
        {
            errMsg = formatExceedError(aNumberOfTangen_CN, this->pIndexWrapper->indexCount2);
        }
        // strncpy(this->pAnalInfo->errMsg, errMsg, sizeof(this->pAnalInfo->errMsg));
        snprintf(this->pAnalInfo->errMsg, sizeof(this->pAnalInfo->errMsg), "%s", errMsg);
        this->pAnalInfo->code = ANAL_ERROR;
        result = false;
    }
    return result;
}

// sub_10005E62
void CResultCaculator::setTickmarkGoPrev(int32_t idx)
{
    int32_t n;
    TDLLResultListItem *pRes = (TDLLResultListItem *)this->head;

    n = idx + this->baseIdx;
    if (n && !typeCode1Without84_116(&pRes[n], 0))
    {
        if (pRes[n - 1].te == pRes[n - 1].tr)
        {
            this->pCTickmarkManager->setFirstTickmark((int32_t)(pRes[n - 1].tr), APEX_TICK);
            pRes[n - 1].TypeCode[2] = C32;
            this->goPrevAfterClear();
            this->tickmarkStatus = false;
            this->pAnalInfo->status = 1;
        }
        else
        {
            this->pCTickmarkManager->setFirstTickmark((int32_t)(pRes[n - 1].hs), VALLEY_TICK);
            this->processIndexAndTickmark(idx - 1);
        }
    }
    else
    {
        this->pCTickmarkManager->setFirstTickmark(pRes[n].ts, START_TICK);
        if (n)
        {
            if (this->isIndex2Valid_())
            {
                this->pIndexWrapper->indexBuf2->tail2 = -1;
                this->pIndexWrapper->indexBuf2->head2 = -1;
                --this->pIndexWrapper->currentTail2;
            }
            this->goPrevAfterClear();
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
            this->pAnalInfo->status = 3;
        }
        else
        {
            memset(this->head, 0, sizeof(TDLLResultListItem));
            this->tickmarkStatus = false;
            this->typeCodeStatus = false;
            this->pAnalInfo->status = 0;
        }
    }
}

// sub_1000610C
void CResultCaculator::setTickmarkRecursively(int32_t idx)
{
    int32_t n;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    n = idx + this->baseIdx;
    this->pCTickmarkManager->setFirstTickmark((int32_t)pHead[n].tr, APEX_TICK);
    if (n && !typeCode1Without84_116(&pHead[n], 0))
    {
        if ((int32_t)pHead[n].tr == pHead[n].ts)
        {
            this->processIndexAndTickmark(idx - 1);
        }
        else if (pHead[n - 1].te == (int32_t)pHead[n - 1].tr)
        {
            this->setTickmarkRecursively(idx - 1);
            this->goPrevAfterClear();
        }
        else
        {
            this->pCTickmarkManager->setFirstTickmark(pHead[n - 1].te, VALLEY_TICK);
            this->goPrevAfterClear();
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
        }
    }
    else
    {
        this->pCTickmarkManager->setFirstTickmark(pHead[n].ts, START_TICK);
        if (n)
        {
            if (this->isIndex2Valid_())
            {
                this->pIndexWrapper->indexBuf2->tail2 = -1;
                this->pIndexWrapper->indexBuf2->head2 = -1;
                --this->pIndexWrapper->currentTail2;
            }
            this->goPrevAfterClear();
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
            this->pAnalInfo->status = 0;
        }
        else
        {
            memset(this->head, 0, sizeof(TDLLResultListItem));
            this->tickmarkStatus = false;
            this->typeCodeStatus = false;
            this->pAnalInfo->status = 0;
        }
    }
}

// sub_10006326
int32_t CResultCaculator::setTickmarkRecursivelyV2(int32_t tail)
{
    int32_t n;
    int32_t result;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    result = 0;
    n = tail + this->baseIdx;
    this->pCTickmarkManager->setFirstTickmark((int32_t)(pHead[n].tr), APEX_TICK);
    if (n && !typeCode1Without84_116(&pHead[n], 0))
    {
        this->pCTickmarkManager->setFirstTickmark(pHead[n].ts, VALLEY_TICK);
        if (pHead[n].tr == pHead[n].ts)
        {
            pHead[n - 1].te = pHead[n].te;
            pHead[n - 1].he = pHead[n].he;
            pHead[n - 1].Areae = pHead[n].Areae;
            this->goPrevAfterClear();
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
        }
        else if (pHead[n - 1].te == pHead[n - 1].tr)
        {
            result = this->setTickmarkRecursivelyV2(tail - 1);
            this->goPrevAfterClear();
        }
        else
        {
            pHead[n - 1].te = pHead[n].te;
            pHead[n - 1].he = pHead[n].he;
            pHead[n - 1].Areae = pHead[n].Areae;
            if (!this->isTHValueInitialized(tail - 1) && ((!this->pEventManager->topOrBottom && pHead[n].hr > pHead[n - 1].hr) || (this->pEventManager->topOrBottom && pHead[n].hr < pHead[n - 1].hr)))
            {
                pHead[n - 1].tr = pHead[n].tr;
                pHead[n - 1].hr = pHead[n].hr;
                pHead[n - 1].Arear = pHead[n].Arear;
            }
            this->goPrevAfterClear();
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
        }
    }
    else
    {
        this->pCTickmarkManager->setFirstTickmark(pHead[n].ts, START_TICK);
        if (n)
        {
            if (this->isIndex2Valid_())
            {
                this->pIndexWrapper->indexBuf2->tail2 = -1;
                this->pIndexWrapper->indexBuf2->head2 = -1;
                --this->pIndexWrapper->currentTail2;
            }
            this->typeCodeStatus = false;
            this->tickmarkStatus = true;
            this->goPrevAfterClear();
        }
        else
        {
            memset(this->head, 0, sizeof(TDLLResultListItem));
            this->typeCodeStatus = false;
            this->tickmarkStatus = false;
            this->pAnalInfo->status = 0;
        }
        result = true;
    }
    return result;
}

// sub_10006A23
void CResultCaculator::setAlternateRES()
{
    if (this->movePointerNext())
    {
        this->pTDLLItem[-1].tr = (float)this->pBuffer10Wrapper->getUnit1TimeEnd();
        this->pTDLLItem[-1].te = (int32_t)this->pTDLLItem[-1].tr;
        this->pTDLLItem[0].ts = this->pTDLLItem[-1].te;
        this->pTDLLItem[-1].hr = (float)(this->pBuffer10Wrapper->getUnit1Height());
        this->pTDLLItem[-1].he = this->pTDLLItem[-1].hr;
        this->pTDLLItem[0].hs = this->pTDLLItem[-1].he;
        this->pTDLLItem[-1].Arear = this->pBuffer10Wrapper->getUnit1Area();
        this->pTDLLItem[-1].Areae = this->pTDLLItem[-1].Arear;
        this->pTDLLItem[0].Areas = this->pTDLLItem[-1].Areae;
        this->typeCodeStatus = false;
    }
}

// sub_10006B22
int32_t CResultCaculator::setAlternateERS()
{
    int32_t result;

    result = this->movePointerNext();
    if (result)
    {
        this->pTDLLItem[-1].te = this->pBuffer10Wrapper->getUnit2TimeEnd();
        this->pTDLLItem[0].tr = (float)(this->pTDLLItem[-1].te);
        this->pTDLLItem[0].ts = (int32_t)this->pTDLLItem[0].tr;
        this->pTDLLItem[-1].he = (float)(this->pBuffer10Wrapper->getUnit2Height());
        this->pTDLLItem[0].hr = this->pTDLLItem[-1].he;
        this->pTDLLItem[0].hs = this->pTDLLItem[0].hr;
        this->pTDLLItem[-1].Areae = this->pBuffer10Wrapper->getType1Counter();
        this->pTDLLItem[0].Arear = this->pTDLLItem[-1].Areae;
        this->pTDLLItem[0].Areas = this->pTDLLItem[0].Arear;
        result = this->setFirstApexTick(0);
    }
    return result;
}

// sub_10006C1B
int32_t CResultCaculator::searchIndexAndCalcValueEX()
{
    int32_t hs;
    int32_t i;
    int32_t time;
    int32_t bufNo;
    int32_t topBottomNo;

    bufNo = 7;
    if (this->baseIdx <= 0)
    {
        for (i = 9; i > 0 && this->pBuffer10Wrapper->getBuffer10Time(i) <= 0; --i)
        {
            ;
        }
        time = this->pBuffer10Wrapper->getBuffer10Time(i);
    }
    else
    {
        time = this->pTDLLItem[-1].te;
    }
    while (bufNo > 0 && this->pBuffer10Wrapper->getBuffer10Time(bufNo) < time)
    {
        --bufNo;
    }
    if (this->pEventManager->topOrBottom)
    {
        topBottomNo = this->pBuffer10Wrapper->searchMaximalHeightIndex(bufNo, 0);
    }
    else
    {
        topBottomNo = this->pBuffer10Wrapper->searchMinimalHeightIndex(bufNo, 0);
    }
    if (this->baseIdx || this->isHeadPointBeginning() || this->pBuffer10Wrapper->getBuffer10Time(topBottomNo) > this->pTDLLItem[-1].te)
    {
        this->setStartFromBuffer10(topBottomNo, 0);
    }
    else
    {
        this->pTDLLItem[0].Areas = this->pTDLLItem[-1].Areae;
        this->pTDLLItem[0].ts = this->pTDLLItem[-1].te;
        this->pTDLLItem[0].hs = this->pTDLLItem[-1].he;
    }
    if (this->isIndexPositive())
    {
        hs = this->calcCurrentPrevAreaTimeHeight(this->pEventManager->topOrBottom);
    }
    else
    {
        hs = (int32_t)this->pTDLLItem[0].hs;
        this->topBottomHS = hs;
    }
    return hs;
}

// sub_1001A5F0
int32_t CResultCaculator::isHeadPointBeginning()
{
    return this->head == pResInfWrapper->getHead();
}

// sub_10004AA4
int32_t CResultCaculator::calcCurrentPrevAreaTimeHeight(int32_t topOrBottom)
{
    double sumArea;
    int32_t j;
    int32_t maxRawValue;
    int32_t prevTe;
    int32_t i;
    int32_t l;
    int32_t k;
    int32_t isPrevTrEqualTe;

    if (!this->isIndexPositive())
    {
        return 0;
    }
    isPrevTrEqualTe = this->isTrEqualTeV1(-1);
    if (topOrBottom)
    {
        prevTe = this->pTDLLItem[-1].te;
        maxRawValue = this->pRawBagBuffer[prevTe];
        for (i = prevTe + 1; i <= this->pTDLLItem[0].ts; ++i)
        {
            if (this->pRawBagBuffer[i] > maxRawValue)
            {
                prevTe = i;
                maxRawValue = this->pRawBagBuffer[i];
            }
        }
        if (prevTe != this->pTDLLItem[0].ts)
        {
            if (prevTe != this->pTDLLItem[-1].te)
            {
                sumArea = this->pTDLLItem[-1].Areae;
                for (j = this->pTDLLItem[-1].te + 1; j <= prevTe; ++j)
                {
                    sumArea = (double)this->pRawBagBuffer[j] + sumArea;
                }
                this->pTDLLItem[0].ts = prevTe;
                this->pTDLLItem[-1].te = prevTe;
                this->pTDLLItem[0].hs = (float)maxRawValue;
                this->pTDLLItem[-1].he = this->pTDLLItem[0].hs;
                this->pTDLLItem[0].Areas = sumArea;
                this->pTDLLItem[-1].Areae = sumArea;
                goto LABEL_28;
            }
        LABEL_23:
            this->pTDLLItem[0].ts = this->pTDLLItem[-1].te;
            this->pTDLLItem[0].hs = this->pTDLLItem[-1].he;
            this->pTDLLItem[0].Areas = this->pTDLLItem[-1].Areae;
            goto LABEL_28;
        }
    }
    else
    {
        prevTe = this->pTDLLItem[-1].te;
        maxRawValue = this->pRawBagBuffer[prevTe];
        for (k = prevTe + 1; k <= this->pTDLLItem[0].ts; ++k)
        {
            if (this->pRawBagBuffer[k] < maxRawValue)
            {
                prevTe = k;
                maxRawValue = this->pRawBagBuffer[k];
            }
        }
        if (prevTe != this->pTDLLItem[0].ts)
        {
            if (prevTe != this->pTDLLItem[-1].te)
            {
                sumArea = this->pTDLLItem[-1].Areae;
                for (l = this->pTDLLItem[-1].te + 1; l <= prevTe; ++l)
                {
                    sumArea = (double)this->pRawBagBuffer[l] + sumArea;
                }
                this->pTDLLItem[0].ts = prevTe;
                this->pTDLLItem[-1].te = prevTe;
                this->pTDLLItem[0].hs = (float)maxRawValue;
                this->pTDLLItem[-1].he = this->pTDLLItem[0].hs;
                this->pTDLLItem[0].Areas = sumArea;
                this->pTDLLItem[-1].Areae = sumArea;
                goto LABEL_28;
            }
            goto LABEL_23;
        }
    }
    this->pTDLLItem[-1].te = this->pTDLLItem[0].ts;
    this->pTDLLItem[-1].he = this->pTDLLItem[0].hs;
    this->pTDLLItem[-1].Areae = this->pTDLLItem[0].Areas;
LABEL_28:
    if (isPrevTrEqualTe)
    {
        this->pTDLLItem[-1].tr = (float)this->pTDLLItem[-1].te;
        this->pTDLLItem[-1].hr = this->pTDLLItem[-1].he;
        this->pTDLLItem[-1].Arear = this->pTDLLItem[-1].Areae;
    }
    this->checkTailReached(-1);
    return true;
}

// sub_10008316
double CResultCaculator::findAndCalcPeakVaue()
{
    int32_t idx;
    int32_t minHeightIdx;

    for (idx = 4; this->pBuffer10Wrapper->getBuffer10Time(idx) < this->pTDLLItem[0].ts; --idx)
    {
        ;
    }
    minHeightIdx = this->pBuffer10Wrapper->searchMinimalHeightIndex(idx, 0);
    return this->calcPeakVaue(minHeightIdx, 0);
}

// sub_10006DA1
double CResultCaculator::findAndCalcPeakVaueV2()
{
    int32_t idx;
    int32_t minHeightIdx;

    for (idx = 4; idx > 0 && this->pBuffer10Wrapper->getBuffer10Time(idx) < this->pTDLLItem[0].ts; --idx)
    {
        ;
    }
    if (this->pEventManager->topOrBottom)
    {
        minHeightIdx = this->pBuffer10Wrapper->searchMinimalHeightIndex(idx, 0);
    }
    else
    {
        minHeightIdx = this->pBuffer10Wrapper->searchMaximalHeightIndex(idx, 0);
    }
    return this->calcPeakVaue(minHeightIdx, 0);
}

// sub_10004880
double CResultCaculator::calcPeakVaue(int32_t buffNo, int32_t offset)
{
    double area;
    int32_t count;
    int32_t height10;
    int32_t sumTime;
    int32_t time10;
    int32_t j;
    int32_t i;
    uint32_t eventWidth;
    int32_t diffCnt;

    time10 = this->pBuffer10Wrapper->getBuffer10Time(buffNo);
    height10 = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(buffNo);
    area = this->pBuffer10Wrapper->getBuffer10Area(buffNo);
    eventWidth = this->pEventManager->eventWidth;
    diffCnt = time10 - eventWidth;
    sumTime = eventWidth + time10;
    if ((int32_t)(time10 - eventWidth) < 0)
    {
        diffCnt = 0;
    }
    if (this->pTDLLItem[offset].ts > 0 && diffCnt < this->pTDLLItem[offset].ts)
    {
        diffCnt = this->pTDLLItem[offset].ts;
    }
    if (sumTime > this->pAnalExecutor->rawbagOffset - 1)
    {
        sumTime = this->pAnalExecutor->rawbagOffset - 1;
    }
    count = time10;
    while (diffCnt <= sumTime + 1)
    {
        if (this->pEventManager->topOrBottom)
        {
            if (this->pRawBagBuffer[diffCnt] < height10)
            {
                height10 = this->pRawBagBuffer[diffCnt];
                count = diffCnt;
            }
        }
        else if (this->pRawBagBuffer[diffCnt] > height10)
        {
            height10 = this->pRawBagBuffer[diffCnt];
            count = diffCnt;
        }
        ++diffCnt;
    }
    if (count <= time10)
    {
        if (count < time10)
        {
            for (i = count + 1; i <= time10; ++i)
            {
                area = area - this->pRawBagBuffer[i];
            }
        }
    }
    else
    {
        for (j = time10 + 1; j <= count; ++j)
        {
            area = this->pRawBagBuffer[j] + area;
        }
    }
    this->pTDLLItem[offset].tr = (float)count;
    this->pTDLLItem[offset].hr = (float)height10;
    this->pTDLLItem[offset].Arear = area;
    return area;
}

// sub_10007094
void CResultCaculator::setCurrenPeakValue()
{
    int32_t evWidth;
    double mBufArea;
    int32_t mTime;
    int32_t mBufHeight;
    int32_t rbIdx;
    int32_t mBufTime;
    int32_t k;
    int32_t j;
    int32_t i;
    int32_t idx;
    int32_t deltaT;

    for (i = 4; i > 0 && (double)this->pBuffer10Wrapper->getBuffer10Time(i) < this->pTDLLItem[0].tr; --i)
    {
        ;
    }
    if (this->pEventManager->topOrBottom)
    {
        idx = this->pBuffer10Wrapper->searchMinimalHeightIndex(i, 0);
    }
    else
    {
        idx = this->pBuffer10Wrapper->searchMaximalHeightIndex(i, 0);
    }
    mBufTime = this->pBuffer10Wrapper->getBuffer10Time(idx);
    mBufHeight = (int32_t)this->pBuffer10Wrapper->getBuffer10Height(idx);
    mBufArea = this->pBuffer10Wrapper->getBuffer10Area(idx);
    evWidth = this->pEventManager->eventWidth;
    deltaT = mBufTime - evWidth;
    rbIdx = evWidth + mBufTime;
    if ((int32_t)(mBufTime - evWidth) < 0)
    {
        deltaT = 0;
    }
    if (this->pTDLLItem[0].ts > 0 && deltaT < this->pTDLLItem[0].ts)
    {
        deltaT = this->pTDLLItem[0].ts;
    }
    if (rbIdx > this->pAnalExecutor->rawbagOffset - 1)
    {
        rbIdx = this->pAnalExecutor->rawbagOffset - 1;
    }
    mTime = mBufTime;
    while (deltaT < rbIdx)
    {
        if (this->pEventManager->topOrBottom)
        {
            if (this->pRawBagBuffer[deltaT] < mBufHeight)
            {
                mBufHeight = this->pRawBagBuffer[deltaT];
                mTime = deltaT;
            }
        }
        else if (this->pRawBagBuffer[deltaT] > mBufHeight)
        {
            mBufHeight = this->pRawBagBuffer[deltaT];
            mTime = deltaT;
        }
        ++deltaT;
    }
    if (mTime <= mBufTime)
    {
        if (mTime < mBufTime)
        {
            for (j = mTime + 1; j <= mBufTime; ++j)
            {
                mBufArea = mBufArea - (double)this->pRawBagBuffer[j];
            }
        }
    }
    else
    {
        for (k = mBufTime + 1; k <= mTime; ++k)
        {
            mBufArea = (double)this->pRawBagBuffer[k] + mBufArea;
        }
    }
    if ((int32_t)this->pTDLLItem[0].hr >= mBufHeight)
    {
        if (this->pTDLLItem[0].hr == mBufHeight)
        {
            this->pTDLLItem[0].tr = (float)(((double)mTime + this->pTDLLItem[0].tr) / 2.0);
            this->pTDLLItem[0].hr = (float)(((double)mBufHeight + this->pTDLLItem[0].hr) / 2.0);
            this->pTDLLItem[0].Arear = (mBufArea + this->pTDLLItem[0].Arear) / 2.0;
        }
    }
    else
    {
        this->pTDLLItem[0].tr = (float)mTime;
        this->pTDLLItem[0].hr = (float)mBufHeight;
        this->pTDLLItem->Arear = mBufArea;
    }
}

// sub_1000737A
int32_t CResultCaculator::setEndValueIfDownSlopeMoreSharp()
{
    int32_t diffFTE;
    int32_t time10;
    int32_t diffT;
    double slopeE;
    double slopeS;
    TDLLResultListItem *pTail;
    TDLLResultListItem *pHead;
    int32_t result;
    TDLLResultListItem *pRes = (TDLLResultListItem *)this->head;

    result = false;
    if (!this->isTail2Zero() && this->pEventManager->tailMode != 2)
    {
        if (this->isIndex2Valid_())
        {
            if (this->isIndex2Valid_())
            {
                diffFTE = (int32_t)this->getDiffFte0AndTr2(-1);
                if (diffFTE > (int32_t)(4 * this->getDiffTr2AndFts2()) || (time10 = this->pBuffer10Wrapper->getBuffer10Time(0),
                                                                           diffT = (int32_t)this->getDiffTr0(time10, -1),
                                                                           diffT > (int32_t)(4 * this->getDiffTr0AndFts0(0))))
                {
                    this->setIndex1EndResult(-1);
                    result = true;
                }
            }
        }
        else
        {
            diffFTE = (int32_t)this->getDiffFte0AndFte1(-1);
            if (diffFTE > (int32_t)(2 * this->getFirstHeadFTimeSpan()))
            {
                this->setIndex1EndResult(-1);
                result = true;
            }
        }
    }
    if (!result && !this->isTHValueInitialized(-1))
    {
        pHead = &pRes[this->pIndexWrapper->indexBuf->head];
        pTail = &this->pTDLLItem[-1];
        if (pTail > pHead)
        {
            if (pTail->ts <= pHead->ts)
            {
                slopeS = 0.0;
            }
            else
            {
                slopeS = (pTail->hs - pHead->hs) / (double)(pTail->ts - pHead->ts);
            }
            if (pTail->te <= pHead->ts)
            {
                slopeE = 0.0;
            }
            else
            {
                slopeE = (pTail->he - pHead->hs) / (double)(pTail->te - pHead->ts);
            }
            if (slopeE >= slopeS)
            {
                this->setIndex1EndResult(-1);
                result = true;
            }
        }
    }
    return result;
}

// sub_1001A620
int32_t CResultCaculator::isTail2Zero()
{
    return this->pIndexWrapper->isCurrentTail2Zero();
}

// sub_1001A6D0
double CResultCaculator::getDiffFte0AndTr2(int32_t offset)
{
    int32_t idx2 = this->pIndexWrapper->indexBuf2->head2;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    return getFte(&this->pTDLLItem[offset]) - pHead[idx2].tr;
}

// sub_1001A7F0
double CResultCaculator::getDiffTr2AndFts2()
{
    int32_t idx2 = this->pIndexWrapper->indexBuf2->head2;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    return pHead[idx2].tr - getFts(&pHead[idx2]);
}

// sub_1001A720
double CResultCaculator::getDiffTr0(double tr, int32_t offset)
{
    return tr - this->pTDLLItem[offset].tr;
}

// sub_1001A750
double CResultCaculator::getDiffTr0AndFts0(int32_t offset)
{
    return this->pTDLLItem[offset].tr - getFts(&this->pTDLLItem[offset]);
}

// sub_1001A660
double CResultCaculator::getDiffFte0AndFte1(int32_t offset)
{
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    return getFte(&this->pTDLLItem[offset]) - getFte(&pHead[this->pIndexWrapper->indexBuf->head]);
}

// sub_10007584
int32_t CResultCaculator::isEventSlopeExceed()
{
    int32_t result;
    float slope;
    int32_t diffH;
    int32_t diffT;
    int32_t eCount;
    int32_t diffT0;

    if (this->pIndexWrapper->baseTail)
    {
        return false;
    }
    diffT = (int32_t)(this->pBuffer10Wrapper->getBuffer10Time(0) - (double)this->pTDLLItem[0].tr);
    diffH = (int32_t)(this->pTDLLItem->hr - this->pBuffer10Wrapper->getBuffer10Height(0));
    if (this->pEventManager->topOrBottom)
    {
        diffH = -diffH;
    }
    eCount = this->pEventManager->eventWidth;
    result = false;
    if (diffT > 0 && eCount > 0 && diffH > 10 * eCount)
    {
        diffT0 = (int32_t)this->pTDLLItem[0].tr - this->pTDLLItem[0].ts;
        slope = (float)((double)diffH / (double)diffT);
        if ((this->pEventManager->getNormalizeSlope() / (double)eCount / 4.0) > slope || (diffT0 > 0 && diffT > 4 * diffT0))
        {
            result = true;
        }
    }
    return result;
}

// sub_10007689
int32_t CResultCaculator::isHeightNormal()
{
    int32_t result;
    int32_t diffHS;
    int32_t diffCurrentHR;
    int32_t diffPrevHR;

    result = false;
    if (this->pEventManager->tailMode == 2)
    {
        if (this->isIndexPositive())
        {
            if (!this->isTrEqualTsV1(0))
            {
                if (this->exceedHalfFirstHR(-1))
                {
                    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
                    if ((BYTE *)this->pTDLLItem > (BYTE *)this->head)
                    {
                        if (this->pTDLLItem[-1].tr > 0)
                        {
                            diffPrevHR = (int32_t)(this->pTDLLItem[-1].hr - pHead->hr);
                            diffHS = (int32_t)(this->pTDLLItem[0].hs - pHead->hr);
                            diffCurrentHR = (int32_t)(this->pTDLLItem[0].hr - pHead->hr);
                            if (this->pEventManager->getSlope() / 2.0 < diffHS && diffPrevHR > 10 * diffCurrentHR && diffCurrentHR < 100 * diffHS)
                            {
                                result = true;
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}

// sub_1001A8F0
int32_t CResultCaculator::exceedHalfFirstHR(int32_t offset)
{
    int32_t result;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    result = this->pIndexWrapper->index1Flag == 0;
    if (this->pIndexWrapper->index1Flag)
    {
        int32_t head = this->pIndexWrapper->indexBuf->head;
        result = pHead[head].hr / 2 < this->pTDLLItem[offset].hr;
    }
    return result;
}

// sub_100077B0
int32_t CResultCaculator::isCurrentPeakHeightExceed()
{
    int32_t diffCurrentHs;
    int32_t diffCurrentHr;
    int32_t diffIndexHr;
    int32_t idxHead = this->pIndexWrapper->indexBuf->head;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    diffIndexHr = (int32_t)(pHead[idxHead].hr - pHead[0].hs);
    diffCurrentHs = (int32_t)(this->pTDLLItem[0].hs - pHead[0].hs);
    diffCurrentHr = (int32_t)(this->pTDLLItem[0].hr - pHead[0].hs);
    if (diffCurrentHs < 0)
    {
        diffCurrentHs = -diffCurrentHs;
    }
    if (diffCurrentHr < 0)
    {
        diffCurrentHr = -diffCurrentHr;
    }
    return (double)diffCurrentHr * 1.5 > (double)diffIndexHr || diffIndexHr <= 2 * (diffCurrentHr - diffCurrentHs) || diffCurrentHr >= 100 * diffCurrentHs;
}

// sub_10007992
int32_t CResultCaculator::lessThenEventDrift()
{
    double absDrift;
    float drift;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    absDrift = this->pEventManager->getEventDrift();
    absDrift = fabs(absDrift);
    if (this->pTDLLItem[0].te <= pHead[-1].ts)
    {
        drift = 0.0;
    }
    else
    {
        drift = (float)((this->pTDLLItem[0].he - pHead[-1].he) / (double)(this->pTDLLItem[0].te - pHead[-1].ts));
    }
    return drift <= (double)absDrift;
}

// sub_10007E74
int32_t CResultCaculator::checkSlopeIsExceed(int32_t time)
{
    int32_t result;
    float slope1;
    float slope0;
    float deltaH1;
    float testHeight;
    float deltaT1;
    float deltaT0;
    float deltaH0;
    float drift;
    float slope;
    float deltaT;

    drift = (float)this->pEventManager->getEventDrift();
    deltaT = (float)(this->pTDLLItem[0].tr - getFts(this->pTDLLItem));
    if (drift > 0.0 && deltaT > 0.0)
    {
        slope = (float)fabs((this->pTDLLItem[0].hr - this->pTDLLItem[0].hs) / deltaT);
        if (this->pEventManager->topOrBottom)
        {
            slope = (float)(0.0 - slope);
        }
        if (slope < drift)
        {
            return true;
        }
    }
    deltaH0 = this->pTDLLItem[0].hr - ((TDLLResultListItem *)this->head)->hs;
    deltaH1 = this->pTDLLItem[0].hr - this->pTDLLItem[0].hs;
    if (this->pEventManager->topOrBottom)
    {
        deltaH0 = (float)(0.0 - deltaH0);
        deltaH1 = (float)(0.0 - deltaH1);
    }
    slope = this->pEventManager->getNormalizeSlope();
    deltaT0 = (float)(this->pTDLLItem->tr - getFts((TDLLResultListItem *)this->head));
    deltaT1 = (float)(this->pTDLLItem->tr - getFts(this->pTDLLItem));
    if (deltaT0 <= 0.0)
    {
        goto LABEL_17;
    }
    if (deltaT1 <= 0.0)
    {
        goto LABEL_17;
    }
    testHeight = (float)((double)time * slope);
    if (deltaH1 > (double)testHeight)
    {
        goto LABEL_17;
    }
    if (!this->isIndexPositive())
    {
        return 1;
    }
    if (10.0 * deltaH0 <= deltaH1 || (slope0 = deltaH0 / deltaT0, slope1 = deltaH1 / deltaT1, slope1 <= (double)slope0))
    {
    LABEL_17:
        result = false;
    }
    else
    {
        result = true;
    }
    return result;
}

// sub_10008180
void CResultCaculator::setSolTickOrIndex2Flag()
{
    if (!this->isIndex2Valid_())
    {
        this->setIndex1EndResult(-1);
    }
    else if (this->pIndexWrapper->currentTail2 > 1)
    {
        this->setSOL_TICK();
    }
    else if (this->baseIdx == this->pIndexWrapper->indexBuf->head + 1)
    {
        this->setSOL_TICK();
    }
    else
    {
        this->pIndexWrapper->index2Flag = 1;
    }
}

// sub_100081DC
int32_t CResultCaculator::restoreEndValueFromBuffer10()
{
    int32_t i;
    int32_t searchedIdx;

    for (i = 4; this->pBuffer10Wrapper->getBuffer10Time(i) < (double)this->pTDLLItem[0].tr; --i)
    {
        ;
    }
    if (this->pEventManager->topOrBottom)
    {
        searchedIdx = this->pBuffer10Wrapper->searchMaximalHeightIndex(i, 0);
    }
    else
    {
        searchedIdx = this->pBuffer10Wrapper->searchMinimalHeightIndex(i, 0);
    }
    return this->finishEndValueAccordingBuffer10(searchedIdx, 0, 0);
}

// sub_100083F8
void CResultCaculator::saveTempValue()
{
    float buffT;
    double slope;
    float deltaT;

    if (this->tempTime <= 0.0 || this->tempTime >= (double)this->pTDLLItem->tr)
    {
        this->pCalcItem = (TDLLResultListItem *)this->head;
    }
    else
    {
        this->pCalcItem = this->pTDLLItem;
        this->tempSlope = 0.0;
        this->tempTime = this->pTDLLItem->tr;
        this->tempHeight = this->pTDLLItem->hr;
        this->tempArea = this->pTDLLItem->Arear;
    }
    buffT = (float)this->pBuffer10Wrapper->getBuffer10Time(4);
    deltaT = (float)(buffT - getFts(this->pCalcItem));
    if (deltaT > 0.0)
    {
        slope = (this->pBuffer10Wrapper->getBuffer10Height(4) - this->pCalcItem->hs) / deltaT;
        if (this->pEventManager->topOrBottom)
        {
            slope = 0.0 - slope;
        }
        if (slope < this->tempSlope)
        {
            this->tempSlope = slope;
            this->tempTime = (float)this->pBuffer10Wrapper->getBuffer10Time(4);
            this->tempHeight = (float)this->pBuffer10Wrapper->getBuffer10Height(4);
            this->tempArea = this->pBuffer10Wrapper->getBuffer10Area(4);
        }
    }
}

// sub_10008579
int32_t CResultCaculator::restoreTempEndValue(int32_t idx)
{
    int32_t result;
    float fte;
    double slope;
    float deltaT;

    slope = 0.0;
    if (!this->pCalcItem)
    {
        this->pCalcItem = (TDLLResultListItem *)this->head;
    }
    fte = (float)getFte(&this->pTDLLItem[idx]);
    deltaT = (float)(fte - getFts(this->pCalcItem));
    if (deltaT > 0.0)
    {
        slope = (this->pTDLLItem[idx].he - this->pCalcItem->hs) / deltaT;
        if (this->pEventManager->topOrBottom)
        {
            slope = 0.0 - slope;
        }
    }
    if (this->isDownSlopeExceedEvDrift(0) || this->tempSlope >= 0.0 || this->tempSlope >= slope || getFte(&this->pTDLLItem[idx]) <= this->tempTime)
    {
        this->clean_();
        result = false;
    }
    else
    {
        this->pTDLLItem[idx].te = (int32_t)this->tempTime;
        this->pTDLLItem[idx].he = this->tempHeight;
        this->pTDLLItem[idx].Areae = this->tempArea;
        if (this->pTDLLItem[idx].fte > 0.0)
        {
            this->pTDLLItem[idx].fte = this->tempTime;
        }
        this->clean_();
        result = true;
    }
    return result;
}

// sub_1000AC2D
int32_t CResultCaculator::isDownSlopeExceedEvDrift(int32_t idx)
{
    double X;
    int32_t result;
    float bufferTime;
    double slope;
    float deltaT;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    bufferTime = (float)this->pBuffer10Wrapper->getBuffer10Time(idx);
    deltaT = (float)(bufferTime - getFts((TDLLResultListItem *)this->head));
    if (deltaT <= 0.0)
    {
        return false;
    }
    slope = (this->pBuffer10Wrapper->getBuffer10Height(idx) - pHead->hs) / deltaT;
    result = false;
    if (slope < 0.0)
    {
        X = this->pEventManager->getEventDrift();
        if (fabs(X) < -slope)
        {
            result = true;
        }
    }
    return result;
}

// sub_1001A360
int32_t CResultCaculator::isIndexPositive()
{
    return this->baseIdx > 0;
}

// sub_1001A380
int32_t CResultCaculator::isTrEqualTeV1(int32_t offset)
{
    int32_t tr;
    tr = (int32_t)this->pTDLLItem[offset].tr;
    return tr && tr == this->pTDLLItem[offset].te;
}

// sub_1001A430
int32_t CResultCaculator::isIndex2Valid_()
{
    return this->pIndexWrapper->isIndex2Valid();
}

// sub_1001A880
int32_t CResultCaculator::isTrEqualTsV1(int32_t offset)
{
    int32_t tr;
    tr = (int32_t)this->pTDLLItem[offset].tr;
    return tr && tr == this->pTDLLItem[offset].ts;
}

// sub_1001B750
int32_t CResultCaculator::isTypeCode83(int32_t idx)
{
    return (this->pTDLLItem[idx].TypeCode[0] & X7F) == C83;
}

// sub_1001B780
int32_t CResultCaculator::isMoreSlope(int32_t idx)
{
    double slope;
    float diff;

    diff = this->pTDLLItem[idx].hr - this->pTDLLItem[idx].he;
    if (this->pEventManager->topOrBottom)
    {
        diff = (float)(0.0 - diff);
    }
    slope = this->pEventManager->getSlope();
    return slope + slope >= diff;
}

// sub_1001B810
CResultCaculator *CResultCaculator::setTickmarkStatus(int32_t status)
{
    this->tickmarkStatus = status;
    return this;
}

// sub_1001BB50
int32_t CResultCaculator::getIndex1Flag()
{
    return this->pIndexWrapper->index1Flag == 1;
}

// sub_1001BB70
int32_t CResultCaculator::isBaseIdxGreatWithoutBaseTail()
{
    return !this->pIndexWrapper->baseTail && (!this->pIndexWrapper->index1Flag || this->baseIdx > this->pIndexWrapper->indexBuf->head);
}

// sub_1001BBC0
int32_t CResultCaculator::isBuff10HeightExceed()
{
    int32_t slopeX10;
    int32_t Number;

    Number = (int32_t)(this->pBuffer10Wrapper->getBuffer10Height(0) - this->pTDLLItem[0].hs);
    slopeX10 = this->pEventManager->getSlopeX10();
    if (this->pEventManager->topOrBottom)
    {
        Number = -Number;
    }
    return labs(Number) > slopeX10;
}

// sub_1001BDF0
int32_t CResultCaculator::isEvSlopeX2GreatThenDH(int32_t idx)
{
    double eventSlope;
    float deltaH;

    deltaH = this->pTDLLItem[idx].hr - this->pTDLLItem[idx].hs;
    if (this->pEventManager->topOrBottom)
    {
        deltaH = (float)(0.0 - deltaH);
    }
    eventSlope = this->pEventManager->getSlope();
    return eventSlope + eventSlope >= deltaH;
}

// sub_1001BE80
int32_t CResultCaculator::isDTGreatThenBufferDT()
{
    float deltaT;

    deltaT = (float)((double)this->pBuffer10Wrapper->getBuffer10Time(0) - this->pTDLLItem[0].tr);
    return this->pTDLLItem[0].tr - getFts(this->pTDLLItem) >= deltaT;
}

// sub_1001BF80
void CResultCaculator::clearTrHr()
{
    this->pTDLLItem[0].tr = 0;
    this->pTDLLItem[0].hr = 0;
}

// sub_1001BFB0
double CResultCaculator::calcSlopeAccordingFTime()
{
    double timeSpan;

    timeSpan = getFte(this->pTDLLItem) - getFts((TDLLResultListItem *)this->head);
    if (timeSpan == 0.0)
    {
        return 0.0;
    }
    return (this->pTDLLItem[0].he - this->pTDLLItem[0].hs) / timeSpan;
}

// sub_1001C020
int32_t CResultCaculator::isEvWidthX10GreatThenDT(int32_t idx)
{
    float deltaT;
    int32_t result;

    result = this->isTrEqualTsV1(idx);
    if (result)
    {
        deltaT = (float)(getFte((TDLLResultListItem *)&this->pTDLLItem[idx]) - this->pTDLLItem[idx].tr);
        result = 10 * this->pEventManager->eventWidth > deltaT;
    }
    return result;
}

// sub_1001C0B0
int32_t CResultCaculator::isBaseIdxGreatThenIndex1Head()
{
    return this->pIndexWrapper->index1Flag && this->baseIdx > this->pIndexWrapper->indexBuf->head;
}

// sub_1001C110
int32_t CResultCaculator::getIndex2Flag()
{
    return this->pIndexWrapper->index2Flag;
}

// sub_1001C130
int32_t CResultCaculator::normalTrendNearEnd()
{
    int32_t result;

    if (this->pEventManager->topOrBottom)
    {
        result = this->pTDLLItem[0].hr < (double)this->pTDLLItem[0].hs && this->pTDLLItem[0].hr < (double)this->pTDLLItem[0].he && (this->pTDLLItem[0].hs - this->pTDLLItem[0].hr) / (this->pTDLLItem[0].he - this->pTDLLItem[0].hr) > 100.0;
    }
    else
    {
        result = this->pTDLLItem[0].hr > (double)this->pTDLLItem[0].hs && this->pTDLLItem[0].hr > (double)this->pTDLLItem[0].he && (this->pTDLLItem[0].hr - this->pTDLLItem[0].hs) / (this->pTDLLItem[0].hr - this->pTDLLItem[0].he) > 100.0;
    }
    return result;
}

// sub_1001C280
int32_t CResultCaculator::checkTypeCode83AndIndex()
{
    return this->tickmarkStatus && !this->isTypeCode83(0) && (!this->isIndex1Valid_() || this->isIndex2Valid_());
}

// sub_1001C2D0
int32_t CResultCaculator::isEvSlopeLitterThenDH()
{
    float deltaH;
    float eventSlope;

    deltaH = (float)(this->pTDLLItem->hr - this->pBuffer10Wrapper->getBuffer10Height(0));
    eventSlope = this->pEventManager->getSlope();
    if (this->pEventManager->topOrBottom)
    {
        deltaH = (float)(0.0 - deltaH);
    }
    return deltaH > (double)eventSlope;
}

// sub_1001C350
int32_t CResultCaculator::isHrGreatThenHalfOfFirst()
{
    return this->pIndexWrapper->baseTail && this->exceedHalfFirstHR(0);
}

// sub_1001C390
int32_t CResultCaculator::isBuffer10HeightStandout()
{
    float diffH;
    int32_t result;

    result = false;
    if (this->isIndex2Valid_())
    {
        TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
        diffH = (float)(pHead[this->pIndexWrapper->indexBuf2->head2].hs - this->pBuffer10Wrapper->getBuffer10Height(0));
        if (this->pEventManager->topOrBottom)
        {
            diffH = (float)(0.0 - diffH);
        }
        if (diffH > 0.0 && this->checkBuffer10DeltaH(&pHead[this->pIndexWrapper->indexBuf2->head2]))
        {
            result = true;
        }
    }
    return result;
}

// sub_10007886
int32_t CResultCaculator::checkBuffer10DeltaH(TDLLResultListItem *pRes)
{
    double diffH04;
    double diffH34;
    double diffH_T4_;
    int32_t diffTime;
    double diffH_T04;
    double diffH_T34;

    if (!pRes)
    {
        pRes = (TDLLResultListItem *)this->head;
    }
    diffTime = this->pBuffer10Wrapper->getBuffer10Time(4) - pRes[0].ts;
    if (!diffTime)
    {
        return false;
    }
    if (this->isDownSlopeExceedEvDrift(4))
    {
        return false;
    }
    diffH_T4_ = (this->pBuffer10Wrapper->getBuffer10Height(4) - pRes[0].hs) / (double)diffTime;

    if (this->pEventManager->evType[PARA_NOISE])
    {
        diffH_T4_ = 0.0 - diffH_T4_;
    }
    diffH34 = this->pBuffer10Wrapper->getBuffer10DiffHeight(3, 4);
    diffH_T34 = diffH34 / (double)this->pBuffer10Wrapper->getBuffer10DiffTime(3, 4);
    diffH04 = this->pBuffer10Wrapper->getBuffer10DiffHeight(0, 4);
    diffH_T04 = diffH04 / (double)this->pBuffer10Wrapper->getBuffer10DiffTime(0, 4);
    return diffH_T34 > diffH_T4_ && diffH_T04 > diffH_T4_;
}

// sub_1001A7A0
double CResultCaculator::getFirstHeadFTimeSpan()
{
    float fte;
    int32_t idx = this->pIndexWrapper->indexBuf->head;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;
    fte = (float)getFte(&pHead[idx]);
    return (double)(fte - getFts(&pHead[idx]));
}

// sub_1000826C
int32_t CResultCaculator::searchIndexAndCalcValue()
{
    int32_t hs;
    int32_t te;
    int32_t buffNo;
    int32_t topCeilIndex;

    buffNo = 7;
    if (this->baseIdx <= 0)
    {
        te = this->storedTE;
    }
    else
    {
        te = this->pTDLLItem[-1].te;
    }
    while (this->pBuffer10Wrapper->getBuffer10Time(buffNo) < te)
    {
        --buffNo;
    }
    topCeilIndex = this->pBuffer10Wrapper->searchMaximalHeightIndex(buffNo, 3);
    this->setStartFromBuffer10(topCeilIndex, 0);
    if (this->isIndexPositive())
    {
        hs = this->calcCurrentPrevAreaTimeHeight(1);
    }
    else
    {
        hs = (int32_t)this->pTDLLItem[0].hs;
        this->topBottomHS = hs;
    }
    return hs;
}

// sub_1001B880
int32_t CResultCaculator::isBuffer0TimeExceed()
{
    float diffTime;
    float time0;
    int32_t result;

    result = this->isIndexPositive();
    if (result)
    {
        time0 = (float)this->pBuffer10Wrapper->getBuffer10Time(0);
        diffTime = (float)(time0 - getFts((TDLLResultListItem *)this->pTDLLItem));
        result = getFte(&this->pTDLLItem[-1]) - this->pTDLLItem[-1].tr < diffTime;
    }
    return result;
}

// sub_10007D60
int32_t CResultCaculator::setPeakEndTypeNB(float time, float height, double area)
{
    float bufHeight;
    float currHs;
    float currHr;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (this->pTDLLItem[0].hs <= 0.0 || this->pTDLLItem[0].hr <= 0.0)
    {
        return false;
    }
    currHs = this->pTDLLItem[0].hs;
    currHr = this->pTDLLItem[0].hr;
    bufHeight = (float)this->pBuffer10Wrapper->getBuffer10Height(0);
    if (currHs <= (double)currHr || bufHeight <= (double)currHr)
    {
        return false;
    }
    this->pTDLLItem[0].te = (int32_t)time;
    this->pTDLLItem[0].he = (float)height;
    this->pTDLLItem[0].Areae = area;
    this->pTDLLItem[0].TypeCode[0] = C78;
    this->pTDLLItem[0].TypeCode[1] = C66;
    this->processIndexAndTickmark(0);
    this->calcResultAndSetBaseIdx();
    return true;
}

// sub_10007A46
int32_t CResultCaculator::setPeakAllTypeNB(int32_t time, float height, double area)
{
    int32_t i;
    int32_t j;
    double currAreas;
    float accHs = 0.0;
    float slope;
    float bufHeight;
    int32_t headTs;
    int32_t currTs;
    float headHs;
    float currHs;
    float currHr;
    float currHe;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (!this->isIndexPositive())
    {
        return false;
    }
    if (!this->isDriftExceedEventDrift(this->pTDLLItem))
    {
        return false;
    }
    headHs = pHead->hs;
    currHs = this->pTDLLItem[0].hs;
    currHr = this->pTDLLItem[-1].hr;
    currHe = this->pTDLLItem[-1].he;
    bufHeight = (float)this->pBuffer10Wrapper->getBuffer10Height(0);
    if (currHe > (double)headHs)
    {
        return false;
    }
    if (headHs - currHe < 10.0)
    {
        return false;
    }
    if (this->pEventManager->getSlope() * 5.0 > headHs - currHe)
    {
        return false;
    }
    if (bufHeight <= (double)currHs || bufHeight >= (double)currHr || headHs <= (double)currHs || headHs >= (double)currHr)
    {
        return false;
    }
    headTs = pHead->ts;
    currTs = this->pTDLLItem[0].ts;
    if (time <= headTs)
    {
        return false;
    }
    for (i = this->pTDLLItem[0].ts; i > (double)this->pTDLLItem[-1].tr; --i)
    {
        slope = (float)((height - headHs) / (double)(time - headTs));
        accHs = (float)((double)(i - headTs) * slope + headHs);
        if ((double)this->pRawBagBuffer[i] >= accHs)
        {
            break;
        }
    }
    currAreas = this->pTDLLItem[0].Areas;
    for (j = this->pTDLLItem[0].ts; j > i; --j)
    {
        currAreas = currAreas - (double)this->pRawBagBuffer[j];
    }
    this->pTDLLItem[-1].te = i;
    this->pTDLLItem[-1].he = accHs;
    this->pTDLLItem[-1].Areae = currAreas;
    this->processIndexAndTickmark(-1);
    this->calcResultAndSetBaseIdx();
    this->pTDLLItem[0].ts = i;
    this->pTDLLItem[0].hs = accHs;
    this->pTDLLItem[0].Areas = currAreas;
    this->pTDLLItem[0].tr = (float)currTs;
    this->pTDLLItem[0].hr = currHs;
    this->pTDLLItem[0].Arear = currAreas;
    this->pTDLLItem[0].te = time;
    this->pTDLLItem[0].he = height;
    this->pTDLLItem[0].Areae = area;
    this->pTDLLItem[0].TypeCode[0] = C78;
    this->pTDLLItem[0].TypeCode[1] = C66;
    this->processIndexAndTickmark(0);
    this->calcResultAndSetBaseIdx();
    return true;
}

// sub_1001C490
int32_t CResultCaculator::isLocalDiffHExtruded8D()
{
    float normSlope;
    float normDiffH05;

    normSlope = this->pEventManager->getNormalizeSlope();
    normDiffH05 = (float)this->pBuffer10Wrapper->getNormalDiffH05();
    if (this->pEventManager->topOrBottom)
    {
        normDiffH05 = (float)(0.0 - normDiffH05);
    }
    return normSlope / 8.0 < normDiffH05;
}

// sub_1001C580
int32_t CResultCaculator::isLocalDiffHExtruded16D_()
{
    float normSlope;
    float normDiffH05;

    normSlope = this->pEventManager->getNormalizeSlope();
    normDiffH05 = (float)this->pBuffer10Wrapper->getNormalDiffH05();
    if (this->pEventManager->topOrBottom)
    {
        normDiffH05 = (float)(0.0 - normDiffH05);
    }
    return -normSlope / 16.0 < normDiffH05;
}

// sub_1000AD12
int32_t CResultCaculator::isDriftExceedEventDrift(TDLLResultListItem *fp)
{
    double eventDrift;
    int32_t result;
    double drift;
    float diffTime;
    TDLLResultListItem *pHead = (TDLLResultListItem *)this->head;

    if (!this->pEventManager->autoNegative || this->pEventManager->topOrBottom)
    {
        return false;
    }
    if (!((int64_t)fp - (int64_t)pHead / 184))
    {
        return false;
    }
    diffTime = (float)(getFts(fp) - getFts(pHead));
    if (diffTime == 0.0)
    {
        return false;
    }
    drift = (fp->hs - pHead->hs) / diffTime;
    result = false;
    if (drift < 0.0)
    {
        eventDrift = this->pEventManager->getEventDrift();
        if (fabs(eventDrift) < -drift)
        {
            result = true;
        }
    }
    return result;
}

// sub_1001C600
CResultCaculator *CResultCaculator::setRawBag(int32_t *pRawBag)
{
    this->pRawBagBuffer = pRawBag;
    return this;
}

// sub_1001DB40
int32_t CResultCaculator::setXMinUnitK(TMiscellaneous *wParam)
{
    if (wParam)
    {
        this->xMinUnitK = wParam->XMinUnitK;
    }
    else
    {
        this->xMinUnitK = 60.0;
    }
    return true;
}
